1

I have multi-dimentional array like below,

$product = array( 
            "2e471a22b1b994a7cb3f3a40cee9fba2" => array (
               "product" => 6004,
               "unique_key" => 3a8a5cb029ee3b92cfc90de23e2329ab,    
               "product_id" => 51,
        "line_total"=>99,
         "quantity"=>1,
        "data"=> array(
        "id"=> 51,
        "post"=>array(
            "ID"=>51,
            "post_title"=>"Prodcut four - control",            
            ),
        "price"=>99    
        )
            ),
    "a7d0f813832ec8a2bf24269ff7145d0c" => array (
               "product" => 6004,
               "unique_key" => c30d1ca26d30aa3dc3c9aa04f0b585ce,    
               "product_id" => 51,
        "line_total"=>99,
        "quantity"=>1,
        "data"=> array(
        "id"=> 51,
        "post"=>array(
            "ID"=>51,
            "post_title"=>"Prodcut four - control",            
            ),
        "price"=>99    
        )
            )
         );

Need to remove the duplicate values based on 'product_id' array value and increase the quantity values based on number of products. Note: The above array have 'unique key' too so any single unique key is needed in array result.

Expected Result should be:
$resultproduct = array( 
        "2e471a22b1b994a7cb3f3a40cee9fba2" => array (
           "product" => 6004,
           "unique_key" => 3a8a5cb029ee3b92cfc90de23e2329ab,    
           "product_id" => 51,
    "line_total"=>99,
     "quantity"=>2,
    "data"=> array(
    "id"=> 51,
    "post"=>array(
        "ID"=>51,
        "post_title"=>"Prodcut four - control",            
        ),
    "price"=>99    
    )
        ));
Ramkumar
  • 23
  • 7
  • Possible duplicate of [How to remove duplicate values from a multi-dimensional array in PHP](http://stackoverflow.com/questions/307674/how-to-remove-duplicate-values-from-a-multi-dimensional-array-in-php) – Syed mohamed aladeen Apr 06 '16 at 11:02
  • Loop product array in foreach then Built the array structure the way you like and increment counter if you find same product id – Mangesh Sathe Apr 06 '16 at 11:05
  • Hey Ramkumar, where is the data coming from? – Mike Apr 06 '16 at 11:06
  • @MangeshSatheIND Need to remove the duplicates value too, all the Products have unique key, so array_unique doesn't works. – Ramkumar Apr 06 '16 at 11:12
  • @SyedMohamedAladeen In above array all the products have unique keys so array_unique not works. The link you given have all the values in a array index are same but it doesn't works in my array as all array index have different unique key. – Ramkumar Apr 06 '16 at 11:16

2 Answers2

0

You need to loop over each product and use the product_id as the key for a new array. This adds the quantities as it goes, so should work for any quantities other than 1.

$result = [];

foreach ($product as $p)
{
    if (isset($result[$p['product_id']]))
    {
        $result[$p['product_id']]['quantity']+= $p['quantity'];
    }

    else
    {
        $result[$p['product_id']] = $p;
    }
}

print_r($result);
iainn
  • 16,826
  • 9
  • 33
  • 40
0

Working code at eval.in

I try and make the code easy to understand so more variables and lines of code than is absolutely required.

Explanation:

1) Need to use the one of original product array index as the output table key e.g. "2e471a22b1b994a7cb3f3a40cee9fba2" for product 51.

2) It needs to be fast relating input productId to the output key. So, I used a lookup table ProductIdList that matches productId to output key.

It is then a two stage lookup to find the entry in the output and add quantities to it.

The code:

// create a product_id => first key table
$productIdList = array();

// output...
$productTotal = array();

foreach ($product as $origIndex => $entry) {

    $curProductId = $entry['product_id'];

    // check product_id exists in the lookup...
    if (isset($productIdList[$curProductId])) { // add to the total...

        $productTotalIndex = $productIdList[$curProductId];

        $productTotal[$productTotalIndex]['quantity'] +=  $entry['quantity'];
    }
    else { // add the entry to the output and the productIdList...

        $productIdList[$curProductId] = $origIndex;

        $productTotal[$origIndex] = $entry;
    }
}

Output: The totals array:

Array
(
    [2e471a22b1b994a7cb3f3a40cee9fba2] => Array
        (
            [product] => 6004
            [unique_key] => 3a8a5cb029ee3b92cfc90de23e2329ab
            [product_id] => 51
            [line_total] => 99
            [quantity] => 2
            [data] => Array
                (
                    [id] => 51
                    [post] => Array
                        (
                            [ID] => 51
                            [post_title] => Prodcut four - control
                        )
                    [price] => 99
                )
        )

    [test02] => Array
        (
            [product] => 6664
            [unique_key] => c30d1ca26d30aa3dc3c9aa04f0b585ce
            [product_id] => 666
            [line_total] => 99
            [quantity] => 579
            [data] => Array
                (
                    [id] => 666
                    [post] => Array
                        (
                            [ID] => 666
                            [post_title] => Prodcut 666 - control
                        )
                    [price] => 99
                )
        )
)

The productId to original key list:

array (size=2)
  51 => string '2e471a22b1b994a7cb3f3a40cee9fba2' (length=32)
  666 => string 'test02' (length=6)
Ryan Vincent
  • 4,483
  • 7
  • 22
  • 31