0

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.

3 Answers3

1
$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);
Bilal Akbar
  • 4,659
  • 1
  • 18
  • 29
  • 2
    Might to add some text to explain your answer. And this code will produce *Undefined index* notices. – Qirel Aug 26 '17 at 12:28
  • Thank you very much for taking your time to answer, this didn't work for me when I first tried however so I gave the correct answer to Qirel as his answer worked before you changed it. Sorry & thanks again. –  Aug 26 '17 at 12:41
1

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

B. Desai
  • 16,414
  • 5
  • 26
  • 47
1

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'];
}
Qirel
  • 25,449
  • 7
  • 45
  • 62