-1

Maybe someone has already asked, but I didn't find the right answer. I need group arrays by mpn and product_id key values and count it's quantities. my array:

    [0] => Array
        (
            [product] => Product HTC
            [mpn] => 
            [quantity] => 3
            [product_id] => 28
        )

    [1] => Array
        (
            [product] => Product HTC
            [mpn] => ggg
            [quantity] => 5
            [product_id] => 28
        )

    [2] => Array
        (
            [product] => Product HTC
            [mpn] => ggg
            [quantity] => 1
            [product_id] => 28
        )

    [3] => Array
        (
            [product] => Product HTC
            [mpn] => ggg
            [quantity] => 1
            [product_id] => 28
        )

    [4] => Array
        (
            [product] => Product HTC
            [mpn] => fff
            [quantity] => 1
            [product_id] => 28
        )

the desired result:

[0] => Array
    (
        [product] => Product HTC
        [mpn] => 
        [quantity] => 3
        [product_id] => 28
    )

[1] => Array
    (
        [product] => Product HTC
        [mpn] => ggg
        [quantity] => 7
        [product_id] => 28
    )


[2] => Array
    (
        [product] => Product HTC
        [mpn] => fff
        [quantity] => 1
        [product_id] => 28
    )

I have tried this suggestion Group array values based on key in php? but no success.

1
  • "I have tried this suggestion [...] but no success." - please show us what you tried then, and give a proper problem description along with it. Commented Dec 1, 2022 at 12:09

2 Answers 2

2

With this code you can obtain an array group by mpn. With $myList is your original array.

// For Each Element
foreach ($myList as $myKey => $myValue)
{
    // Define New Key
    $newKey = $myValue["mpn"];

    // You can Define New Key with Concatenation of Multiple Element
    // Ex : $newKey = $myValue["mpn"]."_".$myValue["product_id"];

    // If Never Memorised OR Already Memorised
    if(!array_key_exists($newKey,$newList)) $newList["$newKey"] = $myValue;
    else $newList["$newKey"]["quantity"] = bcadd($newList["$newKey"]["quantity"],$myValue["quantity"],0);
}
// End - For Each Element

// Display Result
echo "<pre>"; print_r($newList); echo "</pre>";
Sign up to request clarification or add additional context in comments.

Comments

1

Here's a working example using a simple loop.

<?php
function group_by_mpn($array){
    $mpn = array();
    $result = array();
    foreach($array as $key => $value){
        if(!in_array($value['mpn'], $mpn)){
            $mpn[] = $value['mpn'];
            $result[] = $value;
        }else{
            $index = array_search($value['mpn'], $mpn);
            $result[$index]['quantity'] = $result[$index]['quantity'] + $value['quantity'];
        }
    }
    return $result;
}
$array = array(
    array('product' => 'Product HTC', 'mpn' => '', 'quantity' => 3, 'product_id' => 28),
    array('product' => 'Product HTC', 'mpn' => 'ggg', 'quantity' => 5, 'product_id' => 28),
    array('product' => 'Product HTC', 'mpn' => 'ggg', 'quantity' => 1, 'product_id' => 28),
    array('product' => 'Product HTC', 'mpn' => 'ggg', 'quantity' => 1, 'product_id' => 28),
    array('product' => 'Product HTC', 'mpn' => 'fff', 'quantity' => 1, 'product_id' => 28),
);

$result = group_by_mpn($array);
echo json_encode($result);

?>

2 Comments

Your solution may be a bit too complex. But I upvoted because it inspired me to construct my answer. :)
Thanks @Juan, looking at my answer a day later it seems that I already don't understand how it works so indeed it may be a little too complex :)

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.