I have an array generated daily that will have duplicate products in it.
[0] => Array
(
[product_id] => 85
[name] => Widescreen Espresso v6.1
[quantity] => 1
)
[1] => Array
(
[product_id] => 85
[name] => Widescreen Espresso v6.1
[quantity] => 2
)
[2] => Array
(
[product_id] => 114
[name] => Panama Esmerelda Diamond Mountain
[quantity] => 1
)
I want to find duplicate products and total them up in an array that would look like this:
[0] => Array
(
[product_id] => 85
[name] => Widescreen Espresso v6.1
[quantity] => 3
)
[1] => Array
(
[product_id] => 114
[name] => Panama Esmerelda Diamond Mountain
[quantity] => 1
)
UPDATE:
I didn't want to remove the duplicates I want to merge duplicates so that the quantity of the product is added together. I managed to work a solution to it with the help of Meenesh Jain's answer below.
$final_array = array();
foreach($order_data as $item => $item_value) {
$pid = $item_value['product_id'];
if(!isset($final_array[$pid])) {
$final_array[$pid] = $item_value;
} else {
$final_array[$pid]['quantity'] += $item_value['quantity'];
}
}
print_r(array_values($final_array));