如何分割成组POST数组?(How to split POST array in groups?)

2019-10-29 19:48发布

我需要每个阵列中的每个值像人的新阵列添加。

<div class="person">
    <input name="name[]">
    <input name="type[]">
</div>
<div class="person">
    <input name="name[]">
    <input name="type[]">
</div>

可能是两人十点多怎么也是动态的。

$ _ POST回报:

Array( [name] => Array([0] => oliver [1] => tom) [type] => Array([0] => developer [1] => designer) )

我想是这样的两个人分开,但我失败尝试:

$person1 = array(
"name" => "oliver"
"type" => "developer"
)

$person2 = array(
"name" => "tom"
"type" => "designer"
)

Answer 1:

$arr['name'] = $_POST['name'];
$arr['type'] = $_POST['type'];

array_unshift($arr, null);
print_r(call_user_func_array('array_map', $arr));

输出:

Array
(
    [0] => Array
        (
            [0] => Oliver
            [1] => developer
        )

    [1] => Array
        (
            [0] => Tom
            [1] => designer
        )

)


Answer 2:

集团的投入要素,让PHP创建数组为您服务!

<?php

if (isset($_POST['submit'])) {
  echo "<pre>";
  print_r($_POST);
  echo "</pre>";
}

?>
<form method="post">
  <div class="person">
      <input name="person[0][name]" value="Fred">
      <input name="person[0][type]" value="Developer">
  </div>
  <div class="person">
      <input name="person[1][name]" value="Alex">
      <input name="person[1][type]" value="Designer">
  </div>
  <input type="submit" value="submit" name="submit"/>
</form>

http://phpfiddle.org/main/code/syv-xw9



Answer 3:

尝试以下方法:

$people = array(); // define new array

// first loop to find the minimal size array - this will prevent errors down the road.
$minIndex = 0;
foreach($_POST as $key=>$value)
    if(is_array($value)){
        $currentSize = sizeof($value); // get the current data array length
        if(($minIndex==0) || ($minIndex > $currentSize))
            $minIndex = $currentSize; // finds the minimus array length
    }

//populate the $people array
for($i = 0; $i < $minIndex; $i++){ // loop until $minIndex to prevent errors
    $current = array(); // this is the current person
    foreach($_POST as $key=>$value) // loop through all $_POST arrays
        if(is_array($value)) // only if this is an array
            $current[$key] = $value[$i]; // add the current data to the current person

    array_push($people,$current);
}

print_r($people); // print all people

对于你的榜样,这将打印:

Array(
    [0] => Array([name] => oliver [type] => developer) 
    [1] => Array([name] => tom [type] => designer)
)

所以,你可以到达人的细节是这样的: $people[0]['name'] $people[0]['type']等。



Answer 4:

不管有多少个名字和你有种类,喂养两个关联数组到array_map()根据需要将重组/转你的数据。

验证码:( 演示 )(或者,如果你需要在子阵列关联键: 演示 )

var_export(array_map(function($n, $t){return [$n, $t];}, $_POST['name'], $_POST['type']));

例如:

$_POST = ['name' => ['oliver', 'tom'], 'type' => ['developer', 'designer']];

变为:

array (
  0 => 
  array (
    0 => 'oliver',
    1 => 'developer',
  ),
  1 => 
  array (
    0 => 'tom',
    1 => 'designer',
  ),
)

$_POST = ['name' => ['oliver', 'tom', 'mitchel'], 'type' => ['developer', 'designer', 'artist']];

变为:

array (
  0 => 
  array (
    0 => 'oliver',
    1 => 'developer',
  ),
  1 => 
  array (
    0 => 'tom',
    1 => 'designer',
  ),
  2 => 
  array (
    0 => 'mitchel',
    1 => 'artist',
  ),
)


文章来源: How to split POST array in groups?