Merge the sub array if it has the save id value?
- by sophie
I have an array and I want to merge the sub array that have the same id value together:
<?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>';
?>
I GOT:
Array
(
[1] => Array
(
[id] => 1
[id_categorie] => 8
[nb] => 14
)
[2] => Array
(
[id] => 2
[id_categorie] => 10
[nb] => 15
)
)
BUT WHAT I WANT IS :
Array
(
[1] => Array
(
Array
(
"id_categorie" => 1,
"nb" => 18
),
Array
(
"id_categorie" => 8,
"nb" => 14
)
)
[2] => Array
(
[id_categorie] => 10
[nb] => 15
)
)
Anyone could tell me how to do this? thanks