0

Sorry if this is a duplicate but I tried my hardest to find a similar question but I wasn't 100% sure what exactly to search for.

The array below is from an e-commerce site I am creating. Each item has colour variants with a unique item code, however items share a sku. I would like to merge the array by the sku code to calculate the quantity of each item. I have seen ways to merge arrays with the same key but can't find a way to merge these.

Thanks

array(4) {
  ["ITEM-CHAIR-v-V0002"]=>
  array(4) {
    ["itemcode"]=> string(16) "ITEM-CHAIR-v-V0002"
    ["sku"]=>string(8) "ITEM-CHAIR"
    ["quantity"]=>string(1) "4"
    ["price"]=>string(5) "49.99"
   }
 ["ITEM-CHAIR-v-V0003"]=>
 array(4) {
    ["itemcode"]=>string(16) "ITEM-CHAIR-v-V0003"
    ["sku"]=>string(8) "ITEM-CHAIR"
    ["quantity"]=>string(1) "7"
    ["price"]=>string(5) "49.99"
   }
 ["ITEM-KNIFE-v-V0001"]=>
 array(4) {
    ["itemcode"]=>string(22) "ITEM-KNIFE-v-V0001"
    ["sku"]=>string(14) "ITEM-KNIFE"
    ["quantity"]=>string(1) "1"
    ["price"]=>string(5) "45.00"
   }
 ["ITEM-CHAIR-v-V0001"]=>
 array(4) {
    ["itemcode"]=>string(16) "ITEM-CHAIR-v-V0001"
    ["sku"]=>string(8) "ITEM-CHAIR"
    ["quantity"]=>string(1) "4"
    ["price"]=>string(5) "49.99"
  }
}
2
  • Thanks for your quick reply. I have tried iterating through the array and adding the quantity if the sku's match, I am struggling with the logic more than anything though, as what I have tried simply adds up the quantity of ALL the items in the basket, the end result I would like is to basically have an array which contains the sku as the key and the quantity as a value, for example from my array above, the key would be ITEM-CHAIR and the value would be 15 as there are 15 chairs in the basket, although they are a mixture of different variants. Commented Oct 14, 2015 at 9:12
  • If you provide code you have already, you get solution faster. You can also provide expected array. Just extend your question about this informations. Commented Oct 14, 2015 at 9:15

2 Answers 2

2

Iterate over your array and just increment array with current counters if you found that key exist or create new index:

$results = array();
foreach ($inputArray as $item) {
    if (isset($results[$item['sku']])) {
        ++$results[$item['sku']];
    } else {
        $results[$item['sku']] = 1;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, although this doesn't give me the exact thing I was after, which is the total quantity of each sku group, it has pointed me in the right direction so I will mark it as the correct answer. Sorry again if my question was vague.
0

As I understand.

 $resultArray = [];
    foreach ($entryArray as $value){
        $resultArray[$value["sku"]][$value["itemcode"]]=$value;
    } 

Now you get that:

array(2) {
    ["ITEM-CHAIR"]=>{
         array(3){
             ["ITEM-CHAIR-v-V0003"] => array(4){...}
             ["ITEM-CHAIR-v-V0002"] => array(4){...}
             ["ITEM-CHAIR-v-V0001"] => array(4){...}
         }
    }
    ["ITEM-KNIFE"]=>{
         array(1){
              ["ITEM-KNIFE-v-V0001"] => array(4){...}
         }     
    }
}

Maybe you want this. So you can use count($resultArray["ITEM-X"]) to get count of sku. And you also get a better sorted array of your products.

Comments

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.