1

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);
}
2
  • this should work github.com/ramsey/array_column Commented Apr 25, 2017 at 15:35
  • Looks interesting and would work but prefer a simpler solution Commented Apr 25, 2017 at 15:48

1 Answer 1

1

Simple foreach will help you out.

Try this code snippet here

ini_set('display_errors', 1);
$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),
);
$result=array();
foreach($orderProducts as $value)
{
    if(!isset($result[$value["name"]]))
    {
        $result[$value["name"]]=$value;
    }
    else
    {
        $result[$value["name"]]["price"]+=$value["price"];
    }
}
print_r(array_values($result));

Solution 2:

$result=array();
array_map(function($value) use (&$result){
    if(!isset($result[$value["name"]]))
    {
        $result[$value["name"]]=$value;
    }
    else
    {
        $result[$value["name"]]["price"]+=$value["price"];
    }
}, $orderProducts);
print_r(array_values($result));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.