I have multidimensionnel array with different types of items but there are repeated items by name with quantity, I want to count each product total quantity in one array. I have tried some solutions like if(in_array) but no luck !. I also have tried pushing every item in $array and no luck
Array
(
[0] => Array
(
[product] => SUCRE 25 KG
[quantity] => 20
)
...
[3] => Array
(
[product] => lait
[quantity] => 20
)
...
[11] => Array
(
[product] => lait
[quantity] => 6
)
[12] => Array
(
[product] => SUCRE 25 KG
[quantity] => 4
)
[13] => Array
(
[product] => SUCRE 25 KG
[quantity] => 4
)
[14] => Array
(
[product] => lait
[quantity] => 10
)
[15] => Array
(
[product] => SUCRE 25 KG
[quantity] => 20
)
...etc
)
and I want it to output like this
Array
(
[0] => Array
(
[product] => SUCRE 25 KG
[quantity] => 48
)
...
[3] => Array
(
[product] => lait
[quantity] => 36
)
...etc
)
I have my code like this:
$array = array();
while($row = $result->fetch_assoc()){
$v = json_decode($row['order_items'], true);
foreach ($v as $key => $valuex) {
foreach ($valuex as $key => $value) {
array_push($array, array('product' => $value['productname'], 'quantity' => $value['quantity']));
}
}
}
print_r($array);