0

I have an array like that :

$my_arr = array();
array(14) {
[0]=>
    array(4) {
    ["id_dde"]=>
    string(3) "535"
    ["id_station"]=>
    string(3) "130"
    ["id_catalog"]=>
    string(2) "41"
    ["quantity"]=>
    string(2) "50"
  }
  [1]=>
  array(4) {
    ["id_dde"]=>
    string(3) "535"
    ["id_station"]=>
    string(3) "130"
    ["id_catalog"]=>
    string(3) "259"
    ["quantity"]=>
    string(2) "70"
  }

With many indexes like that. My goal is to have a unique value on id_dde, id_station and id_catalog. If I have two indexes with array that have similar values on theses columns, I want to delete one index, and merge both quantity columns. Here is an example :

$my_arr = array();
array(14) {
[0]=>
    array(4) {
    ["id_dde"]=>
    string(3) "535"
    ["id_station"]=>
    string(3) "130"
    ["id_catalog"]=>
    string(2) "41"
    ["quantity"]=>
    string(2) "50"
  }
  [1]=>
  array(4) {
    ["id_dde"]=>
    string(3) "535"
    ["id_station"]=>
    string(3) "130"
    ["id_catalog"]=>
    string(3) "41"
    ["quantity"]=>
    string(2) "5"
  }

=>

array(14) {
[0]=>
    array(4) {
    ["id_dde"]=>
    string(3) "535"
    ["id_station"]=>
    string(3) "130"
    ["id_catalog"]=>
    string(2) "41"
    ["quantity"]=>
    string(2) "55"
  }

How can I do that ? I tried with a double foreach and unset but I didn't get the right answer, I guess there is a better method too ?

Thanks!

AjaxLoser
  • 31
  • 1
  • 9
  • 1
    We are always glad to help and support new coders but ***you need to help yourself first. :-)*** After [**doing more research**](https://meta.stackoverflow.com/q/261592/1011527) if you have a problem **post what you've tried** with a **clear explanation of what isn't working** and provide [a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). Read [How to Ask](http://stackoverflow.com/help/how-to-ask) a good question. Be sure to [take the tour](http://stackoverflow.com/tour) and read [this](https://meta.stackoverflow.com/q/347937/1011527). – Jay Blanchard Aug 09 '18 at 12:52
  • I have found a similar example here https://stackoverflow.com/questions/24150063/in-php-merge-duplicate-set-of-elements-of-an-multidimensional-array-and-sum-the , I didn't found it when I've done my researches... Sorry! :) – AjaxLoser Aug 09 '18 at 12:55

1 Answers1

1

I got an expect result with double foreach. Try this :

$result=[];
foreach ($my_arr as &$dde1) {
    $qty = 0;
    foreach ($my_arr as $key => $dde2) {
        if ($dde1['id_dde'] == $dde2['id_dde'] && $dde1['id_station'] == $dde2['id_station'] && $dde1['id_catalog'] == $dde2['id_catalog']) {
            $qty += $dde2['quantity'];
            unset($my_arr[$key]);
        }
    }
    $dde1['quantity'] = $qty;
    $result[] = $dde1;
}
Jouby
  • 2,196
  • 2
  • 21
  • 33
  • Thank you, I missed a part... I've taken another solution here https://stackoverflow.com/questions/24150063/in-php-merge-duplicate-set-of-elements-of-an-multidimensional-array-and-sum-the but thanks anyway :) – AjaxLoser Aug 09 '18 at 14:39