我有一个数组,我想将具有相同 id 值的子数组合并在一起:
<?php
$a = Array(
Array
(
"id" => 1,
"id_categorie" => 1,
"nb" => 18
),
Array
(
"id" => 1,
"id_categorie" => 8,
"nb" => 14
),
Array
(
"id" => 2,
"id_categorie" => 10,
"nb" => 15
)
);
$result = array();
foreach ($a as $k=>$v){
$result[$v['id']] =$v;
}
echo '<pre>';
print_r($result);
echo '</pre>';
?>
我有:
Array
(
[1] => Array
(
[id] => 1
[id_categorie] => 8
[nb] => 14
)
[2] => Array
(
[id] => 2
[id_categorie] => 10
[nb] => 15
)
)
但我想要的是:
Array
(
[1] => Array
(
Array
(
"id_categorie" => 1,
"nb" => 18
),
Array
(
"id_categorie" => 8,
"nb" => 14
)
)
[2] => Array
(
[id_categorie] => 10
[nb] => 15
)
)
任何人都可以告诉我如何做到这一点?谢谢