这是我的交易。我有这个数组:
Array // called $data in my code
(
[0] => Array
(
[name] => quantity
[value] => 0
)
[1] => Array
(
[name] => var_id
[value] => 4
)
[2] => Array
(
[name] => quantity
[value] => 0
)
[3] => Array
(
[name] => var_id
[value] => 5
)
)
我需要它是这样的:
Array // called $temp in my code
(
[0] => Array
(
[0] => Array
(
[name] => quantity
[value] => 0
)
[1] => Array
(
[name] => var_id
[value] => 4
)
)
[2] => Array
(
[0] => Array
(
[name] => quantity
[value] => 0
)
[1] => Array
(
[name] => var_id
[value] => 5
)
)
)
我使用我制作的这段代码做到了:
$data = $_POST['data'];
$temp = array();
foreach($data as $key => $datum)
{
if($key%2 == 0)
{
$temp[$key] = array();
array_push($temp[$key], $datum, $data[$key+1]);
}
}
但我认为我的代码有点愚蠢,特别是如果我有大量数据。最终我想要做的只是将每两个索引组合在一个数组中,我知道应该有比我的代码更好的东西来做,有什么建议吗?