Q:

Count multidimmensional array values by same array value

Let's assume this scenario. We have multidimmensional $products array like this. 2 products, but 3 attributes and different cart_quantity for every one.
Array
(
    [0] => Array
        (
            [id_product] => 1
            [id_product_attribute] => 5
            [cart_quantity] => 1
        )
    [1] => Array
        (
            [id_product] => 620
            [id_product_attribute] => 784
            [cart_quantity] => 8
        )
    [2] => Array
        (
            [id_product] => 1
            [id_product_attribute] => 12
            [cart_quantity] => 3
        )
)

How I can sum all of $product['cart_quantity'] for each array entry with same id_product that I would have result like this:
Array
(
     [1] => 4 // 1 + 3
     [620] => 8
)

I am really sorry about thread title I can't even form what I want to achieve without showing this in example above.

A:

Use a simple foreach loop. Use $product['product_id'] as the key in the result array, which you simply add to when you get a duplicate ID.
$result = array();
foreach ($products as $product) {
    $id = $product['product_id'];
    $quant = $product['cart_quantity'];
    if (isset($result[$id])) {
        $result[$id] += $quant;
    } else {
        $result[$id] = $quant;
}

