How to split POST array in groups?

China☆狼群 提交于 2019-12-11 19:54:48

问题


I need each value of each array add on new array like person.

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

Could be two persons how also ten o more is dynamic.

$_POST return that:

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

I want something like that the two persons separate, but i fail trying:

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

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

回答1:


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

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

Output:

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

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

)



回答2:


Group your input elements and letting PHP create the arrays for you!

<?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




回答3:


Try the following:

$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

For your example this will print:

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

So you can reach person's details like this: $people[0]['name'], $people[0]['type'], etc..




回答4:


No matter how many names and types you have, feeding the two associative arrays into array_map() will restructure / transpose your data as desired.

Code: (Demo) (or if you need associative keys in the subarrays: Demo)

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

For example:

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

becomes:

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

and

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

becomes:

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


来源:https://stackoverflow.com/questions/17799375/how-to-split-post-array-in-groups

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!