user5762586
user5762586

Reputation:

PHP Loop - Sum the Value of an Array if the Key Exists

I have a multidimensional associative array that I need to loop through and collect the product specific data. Please see below:

$rawData = array(
    array(
        'sku' => 'product1',
        'quantity' => '3'
    ),
    array(
        'sku' => 'product2',
        'quantity' => '3'
    ),
    array(
        'sku' => 'product1',
        'quantity' => '2'
    )
);

$collectedData = array();
for ($i = 0; $i < count($rawData); $i++) {
    $collectedData += array($rawData[$i]['sku'] => $rawData[$i]['quantity']);
}

print_r($collectedData);

This outputs the following:

Array
(
    [product1] => 3
    [product2] => 3
)

What I need however is that if the array key sku already exists in the $collectedData array, for the quantity of the array value to be added to the already existing quantity.

So this would be my desired result (as product1 exists twice with values 2 and 3):

Array
(
    [product1] => 5
    [product2] => 3
)

Is there an easy way of doing this? Thank you very much for any help in advance.

Upvotes: 0

Views: 2864

Answers (3)

Bilal Akbar
Bilal Akbar

Reputation: 4930

$collectedData = array();

foreach($rawData as $data)
{
   // if already exist then add
   if(isset($collectedData[$data['sku']]))
      {
        $collectedData[$data['sku']] += $data['quantity'];
      }
   // else set value
   else
     {
      $collectedData[$data['sku']] = $data['quantity'];
     }    
}

print_r($collectedData);

Upvotes: 1

B. Desai
B. Desai

Reputation: 16436

Simply check key is available or not. if avalable then sum quantity

$rawData = array(
    array(
        'sku' => 'product1',
        'quantity' => '3'
    ),
    array(
        'sku' => 'product2',
        'quantity' => '3'
    ),
    array(
        'sku' => 'product1',
        'quantity' => '2'
    )
);

$collectedData = array();
for ($i = 0; $i < count($rawData); $i++) {
    if(isset($collectedData[$rawData[$i]['sku']]))
        $collectedData[$rawData[$i]['sku']] +=  $rawData[$i]['quantity'];
     else
        $collectedData += array($rawData[$i]['sku'] => $rawData[$i]['quantity']);
}

print_r($collectedData);

Also I suggest you foreach loop for simplification and unneccessary use of indexes

$collectedData = array();
foreach($rawData as $row){
    if(isset($collectedData[$row['sku']]))
        $collectedData[$row['sku']] +=  $row['quantity'];
     else
        $collectedData += array($row['sku'] => $row['quantity']);
}

print_r($collectedData);

DEMO

Upvotes: 1

Qirel
Qirel

Reputation: 26460

Lop the array with foreach, and check if the key exists - if it doesn't, set the value to the quantity, if it does, add it to the existing quantity. The key represents the product. We check if its set or not to avoid having the code produce warnings (as you can see this demo produces).

$collectedData = array();
foreach ($rawData as $value) {
    if (!isset($collectedData[$value['sku']]))
        $collectedData[$value['sku']] = $value['quantity'];
    else
        $collectedData[$value['sku']] += $value['quantity'];
}

Upvotes: 1

Related Questions