0

我需要每个数组的每个值都添加到新数组上,比如人。

<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"
)
4

4 回答 4

2
$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
        )

)
于 2013-07-23T00:43:14.323 回答
1

将您的输入元素分组并让 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

于 2013-07-23T00:22:40.630 回答
0

尝试以下操作:

$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']等。

于 2013-07-23T00:01:48.340 回答
0

无论您有多少名称和类型,将两个关联数组输入到其中array_map()都会根据需要重组/转置您的数据。

代码:(Demo)(或者如果您需要子数组中的关联键:Demo

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',
  ),
)
于 2018-11-03T11:26:06.663 回答