I'm looking to create an unique array based on an array of products, example being:
$orderProducts = array (
array('name' => 'Series 101. Pure Red Sable', 'price' => 4),
array('name' => 'Series 101. Pure Red Sable', 'price' => 6),
array('name' => 'Series 101. Pure Red Sable', 'price' => 8),
array('name' => 'Series 101. Pure Red Sable', 'price' => 10),
array('name' => 'Series 101. Pure Red Sable', 'price' => 12),
array('name' => 'Series 222', 'price' => 5),
array('name' => 'Series 222', 'price' => 5),
array('name' => 'Series 1', 'price' => 7),
array('name' => 'Series 1', 'price' => 7),
);
The objective is to create an array with a single name iteration, with the sum of their prices. For example, all Series 101. Pure Red Sable would be 40. Then, I'd like to put them inside a new unique array, like this:
$newProductsArray = array (
array('name' => 'Series 101. Pure Red Sable', 'price' => 40),
array('name' => 'Series 222', 'price' => 10),
array('name' => 'Series 1', 'price' => 14),
);
I'm using PHP version 5.4.12 but I've manually added the array_column function and experimented with it to reach my goal, with no success. Perhaps it's possible to expand the function to create the desired result?
function array_column($array,$column_name)
{
return array_map(function($element) use($column_name){return $element[$column_name];}, $array);
}