Array merge to multidimensional by key












-1















Iam having trouble with solving merging of array to multidimensional array.



My array looks like:



Array
(
[0] => Array
(
[id] => 41844
[regNumber] => CSM-057.05
[name] => 100n SMD 0603 10% X7R 25V
[madeBy2] => Pro_Sinoskysun International (HK) CO., LTD
)

[1] => Array
(
[id] => 41844
[regNumber] => CSM-057.05
[name] => 100n SMD 0603 10% X7R 25V
[madeBy2] => Pro_RUTRONIK Elektronische Ba
)

[2] => Array
(
[id] => 41844
[regNumber] => CSM-057.05
[name] => 100n SMD 0603 10% X7R 25V
[madeBy2] => Pro_TTI
)

[3] => Array
(
[id] => 41844
[regNumber] => CSM-057.05
[name] => 100n SMD 0603 10% X7R 25V
[madeBy2] => Pro_ARROW CENTRAL EUROPE GMBH
)

[4] => Array
(
[id] => 41844
[regNumber] => CSM-057.05
[name] => 100n SMD 0603 10% X7R 25V
[madeBy2] => Pro_TDK
)

[5] => Array
(
[id] => 41844
[regNumber] => CSM-057.05
[name] => 100n SMD 0603 10% X7R 25V
[madeBy2] => Pro_YANGZHIDING ELECTRONICS
)
[6] => Array
(
[id] => 41301
[regNumber] => P+CSM-302.01
[name] => 100n SMD 0603 5% X7R 25V
[madeBy2] =>
)
[7] => Array
(
[id] => 24526
[regNumber] => P+CSM-097.04
[name] => 100n SMD 0603 10% X7R 25V
[madeBy2] => Pro_TDK
)
)


And i need to merge them like:



Array
(
[0] => Array
(
[id] => 41844
[regNumber] => CSM-057.05
[name] => 100n SMD 0603 10% X7R 25V
[madeBy2] => Array
(
"Pro_Sinoskysun International (HK) CO., LTD",
"Pro_RUTRONIK Elektronische Ba",
"Pro_TTI",
"Pro_ARROW CENTRAL EUROPE GMBH",
"Pro_TDK",
"Pro_YANGZHIDING ELECTRONICS"
)
)
[1] => Array
(
[id] => 41301
[regNumber] => P+CSM-302.01
[name] => 100n SMD 0603 5% X7R 25V
[madeBy2] =>
)
[2] => Array
(
[id] => 24526
[regNumber] => P+CSM-097.04
[name] => 100n SMD 0603 10% X7R 25V
[madeBy2] => Pro_TDK
)
)


Input array can be 5 or 500 items and i need to merge them by ID.. There is just one thing different from each other and its who made them. Is it somehow possible?



If something more is needed i can add it. :)



Thanks a lot for helping.










share|improve this question

























  • Have you tried anything so far?

    – Nigel Ren
    Nov 16 '18 at 7:14











  • I will say that it can be done, and it would not even be that hard. You will have to iterate over the firts array and add element to their respective places in second array during the iteration of the first

    – Eugene Anisiutkin
    Nov 16 '18 at 7:20











  • @NigelRen yep i've.. also i've tried to looking on stack for answers too but nothing from it was working as i wanted.. :( Iam learning by myself so its lil harder for me to solve things by myself.. I tried it for few hrs but with no result

    – Daniel Sympho Weiss
    Nov 16 '18 at 7:22


















-1















Iam having trouble with solving merging of array to multidimensional array.



My array looks like:



Array
(
[0] => Array
(
[id] => 41844
[regNumber] => CSM-057.05
[name] => 100n SMD 0603 10% X7R 25V
[madeBy2] => Pro_Sinoskysun International (HK) CO., LTD
)

[1] => Array
(
[id] => 41844
[regNumber] => CSM-057.05
[name] => 100n SMD 0603 10% X7R 25V
[madeBy2] => Pro_RUTRONIK Elektronische Ba
)

[2] => Array
(
[id] => 41844
[regNumber] => CSM-057.05
[name] => 100n SMD 0603 10% X7R 25V
[madeBy2] => Pro_TTI
)

[3] => Array
(
[id] => 41844
[regNumber] => CSM-057.05
[name] => 100n SMD 0603 10% X7R 25V
[madeBy2] => Pro_ARROW CENTRAL EUROPE GMBH
)

[4] => Array
(
[id] => 41844
[regNumber] => CSM-057.05
[name] => 100n SMD 0603 10% X7R 25V
[madeBy2] => Pro_TDK
)

[5] => Array
(
[id] => 41844
[regNumber] => CSM-057.05
[name] => 100n SMD 0603 10% X7R 25V
[madeBy2] => Pro_YANGZHIDING ELECTRONICS
)
[6] => Array
(
[id] => 41301
[regNumber] => P+CSM-302.01
[name] => 100n SMD 0603 5% X7R 25V
[madeBy2] =>
)
[7] => Array
(
[id] => 24526
[regNumber] => P+CSM-097.04
[name] => 100n SMD 0603 10% X7R 25V
[madeBy2] => Pro_TDK
)
)


And i need to merge them like:



Array
(
[0] => Array
(
[id] => 41844
[regNumber] => CSM-057.05
[name] => 100n SMD 0603 10% X7R 25V
[madeBy2] => Array
(
"Pro_Sinoskysun International (HK) CO., LTD",
"Pro_RUTRONIK Elektronische Ba",
"Pro_TTI",
"Pro_ARROW CENTRAL EUROPE GMBH",
"Pro_TDK",
"Pro_YANGZHIDING ELECTRONICS"
)
)
[1] => Array
(
[id] => 41301
[regNumber] => P+CSM-302.01
[name] => 100n SMD 0603 5% X7R 25V
[madeBy2] =>
)
[2] => Array
(
[id] => 24526
[regNumber] => P+CSM-097.04
[name] => 100n SMD 0603 10% X7R 25V
[madeBy2] => Pro_TDK
)
)


Input array can be 5 or 500 items and i need to merge them by ID.. There is just one thing different from each other and its who made them. Is it somehow possible?



If something more is needed i can add it. :)



Thanks a lot for helping.










share|improve this question

























  • Have you tried anything so far?

    – Nigel Ren
    Nov 16 '18 at 7:14











  • I will say that it can be done, and it would not even be that hard. You will have to iterate over the firts array and add element to their respective places in second array during the iteration of the first

    – Eugene Anisiutkin
    Nov 16 '18 at 7:20











  • @NigelRen yep i've.. also i've tried to looking on stack for answers too but nothing from it was working as i wanted.. :( Iam learning by myself so its lil harder for me to solve things by myself.. I tried it for few hrs but with no result

    – Daniel Sympho Weiss
    Nov 16 '18 at 7:22
















-1












-1








-1








Iam having trouble with solving merging of array to multidimensional array.



My array looks like:



Array
(
[0] => Array
(
[id] => 41844
[regNumber] => CSM-057.05
[name] => 100n SMD 0603 10% X7R 25V
[madeBy2] => Pro_Sinoskysun International (HK) CO., LTD
)

[1] => Array
(
[id] => 41844
[regNumber] => CSM-057.05
[name] => 100n SMD 0603 10% X7R 25V
[madeBy2] => Pro_RUTRONIK Elektronische Ba
)

[2] => Array
(
[id] => 41844
[regNumber] => CSM-057.05
[name] => 100n SMD 0603 10% X7R 25V
[madeBy2] => Pro_TTI
)

[3] => Array
(
[id] => 41844
[regNumber] => CSM-057.05
[name] => 100n SMD 0603 10% X7R 25V
[madeBy2] => Pro_ARROW CENTRAL EUROPE GMBH
)

[4] => Array
(
[id] => 41844
[regNumber] => CSM-057.05
[name] => 100n SMD 0603 10% X7R 25V
[madeBy2] => Pro_TDK
)

[5] => Array
(
[id] => 41844
[regNumber] => CSM-057.05
[name] => 100n SMD 0603 10% X7R 25V
[madeBy2] => Pro_YANGZHIDING ELECTRONICS
)
[6] => Array
(
[id] => 41301
[regNumber] => P+CSM-302.01
[name] => 100n SMD 0603 5% X7R 25V
[madeBy2] =>
)
[7] => Array
(
[id] => 24526
[regNumber] => P+CSM-097.04
[name] => 100n SMD 0603 10% X7R 25V
[madeBy2] => Pro_TDK
)
)


And i need to merge them like:



Array
(
[0] => Array
(
[id] => 41844
[regNumber] => CSM-057.05
[name] => 100n SMD 0603 10% X7R 25V
[madeBy2] => Array
(
"Pro_Sinoskysun International (HK) CO., LTD",
"Pro_RUTRONIK Elektronische Ba",
"Pro_TTI",
"Pro_ARROW CENTRAL EUROPE GMBH",
"Pro_TDK",
"Pro_YANGZHIDING ELECTRONICS"
)
)
[1] => Array
(
[id] => 41301
[regNumber] => P+CSM-302.01
[name] => 100n SMD 0603 5% X7R 25V
[madeBy2] =>
)
[2] => Array
(
[id] => 24526
[regNumber] => P+CSM-097.04
[name] => 100n SMD 0603 10% X7R 25V
[madeBy2] => Pro_TDK
)
)


Input array can be 5 or 500 items and i need to merge them by ID.. There is just one thing different from each other and its who made them. Is it somehow possible?



If something more is needed i can add it. :)



Thanks a lot for helping.










share|improve this question
















Iam having trouble with solving merging of array to multidimensional array.



My array looks like:



Array
(
[0] => Array
(
[id] => 41844
[regNumber] => CSM-057.05
[name] => 100n SMD 0603 10% X7R 25V
[madeBy2] => Pro_Sinoskysun International (HK) CO., LTD
)

[1] => Array
(
[id] => 41844
[regNumber] => CSM-057.05
[name] => 100n SMD 0603 10% X7R 25V
[madeBy2] => Pro_RUTRONIK Elektronische Ba
)

[2] => Array
(
[id] => 41844
[regNumber] => CSM-057.05
[name] => 100n SMD 0603 10% X7R 25V
[madeBy2] => Pro_TTI
)

[3] => Array
(
[id] => 41844
[regNumber] => CSM-057.05
[name] => 100n SMD 0603 10% X7R 25V
[madeBy2] => Pro_ARROW CENTRAL EUROPE GMBH
)

[4] => Array
(
[id] => 41844
[regNumber] => CSM-057.05
[name] => 100n SMD 0603 10% X7R 25V
[madeBy2] => Pro_TDK
)

[5] => Array
(
[id] => 41844
[regNumber] => CSM-057.05
[name] => 100n SMD 0603 10% X7R 25V
[madeBy2] => Pro_YANGZHIDING ELECTRONICS
)
[6] => Array
(
[id] => 41301
[regNumber] => P+CSM-302.01
[name] => 100n SMD 0603 5% X7R 25V
[madeBy2] =>
)
[7] => Array
(
[id] => 24526
[regNumber] => P+CSM-097.04
[name] => 100n SMD 0603 10% X7R 25V
[madeBy2] => Pro_TDK
)
)


And i need to merge them like:



Array
(
[0] => Array
(
[id] => 41844
[regNumber] => CSM-057.05
[name] => 100n SMD 0603 10% X7R 25V
[madeBy2] => Array
(
"Pro_Sinoskysun International (HK) CO., LTD",
"Pro_RUTRONIK Elektronische Ba",
"Pro_TTI",
"Pro_ARROW CENTRAL EUROPE GMBH",
"Pro_TDK",
"Pro_YANGZHIDING ELECTRONICS"
)
)
[1] => Array
(
[id] => 41301
[regNumber] => P+CSM-302.01
[name] => 100n SMD 0603 5% X7R 25V
[madeBy2] =>
)
[2] => Array
(
[id] => 24526
[regNumber] => P+CSM-097.04
[name] => 100n SMD 0603 10% X7R 25V
[madeBy2] => Pro_TDK
)
)


Input array can be 5 or 500 items and i need to merge them by ID.. There is just one thing different from each other and its who made them. Is it somehow possible?



If something more is needed i can add it. :)



Thanks a lot for helping.







php arrays multidimensional-array






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 7:37







Daniel Sympho Weiss

















asked Nov 16 '18 at 7:08









Daniel Sympho WeissDaniel Sympho Weiss

64




64













  • Have you tried anything so far?

    – Nigel Ren
    Nov 16 '18 at 7:14











  • I will say that it can be done, and it would not even be that hard. You will have to iterate over the firts array and add element to their respective places in second array during the iteration of the first

    – Eugene Anisiutkin
    Nov 16 '18 at 7:20











  • @NigelRen yep i've.. also i've tried to looking on stack for answers too but nothing from it was working as i wanted.. :( Iam learning by myself so its lil harder for me to solve things by myself.. I tried it for few hrs but with no result

    – Daniel Sympho Weiss
    Nov 16 '18 at 7:22





















  • Have you tried anything so far?

    – Nigel Ren
    Nov 16 '18 at 7:14











  • I will say that it can be done, and it would not even be that hard. You will have to iterate over the firts array and add element to their respective places in second array during the iteration of the first

    – Eugene Anisiutkin
    Nov 16 '18 at 7:20











  • @NigelRen yep i've.. also i've tried to looking on stack for answers too but nothing from it was working as i wanted.. :( Iam learning by myself so its lil harder for me to solve things by myself.. I tried it for few hrs but with no result

    – Daniel Sympho Weiss
    Nov 16 '18 at 7:22



















Have you tried anything so far?

– Nigel Ren
Nov 16 '18 at 7:14





Have you tried anything so far?

– Nigel Ren
Nov 16 '18 at 7:14













I will say that it can be done, and it would not even be that hard. You will have to iterate over the firts array and add element to their respective places in second array during the iteration of the first

– Eugene Anisiutkin
Nov 16 '18 at 7:20





I will say that it can be done, and it would not even be that hard. You will have to iterate over the firts array and add element to their respective places in second array during the iteration of the first

– Eugene Anisiutkin
Nov 16 '18 at 7:20













@NigelRen yep i've.. also i've tried to looking on stack for answers too but nothing from it was working as i wanted.. :( Iam learning by myself so its lil harder for me to solve things by myself.. I tried it for few hrs but with no result

– Daniel Sympho Weiss
Nov 16 '18 at 7:22







@NigelRen yep i've.. also i've tried to looking on stack for answers too but nothing from it was working as i wanted.. :( Iam learning by myself so its lil harder for me to solve things by myself.. I tried it for few hrs but with no result

– Daniel Sympho Weiss
Nov 16 '18 at 7:22














3 Answers
3






active

oldest

votes


















1














This code will produce your desired output, regardless of whether the array has different id values. This code does assume that all similar products are grouped in order though:



$output = array();
$i = -1;
foreach ($array as $arr) {
if ($i < 0 || $output[$i]['id'] != $arr['id'] || $output[$i]['regNumber'] != $arr['regNumber'] || $output[$i]['name'] != $arr['name']) {
// new product
$output[++$i] = $arr;
$output[$i]['madeBy2'] = array($arr['madeBy2']);
}
else {
// same product, new manufacturer
$output[$i]['madeBy2'] = $arr['madeBy2'];
}
}
print_r($output);


Demo on 3v4l.org



Alternate version Only makes madeBy2 element into an array if there are multiple values:



$output = array();
$i = -1;
foreach ($array as $arr) {
if ($i < 0 || $output[$i]['id'] != $arr['id'] || $output[$i]['regNumber'] != $arr['regNumber'] || $output[$i]['name'] != $arr['name']) {
// new product
$output[++$i] = $arr;
}
else {
// same product, new manufacturer
if (!is_array($output[$i]['madeBy2']))
$output[$i]['madeBy2'] = array($output[$i]['madeBy2'], $arr['madeBy2']);
else
$output[$i]['madeBy2'] = $arr['madeBy2'];
}
}
print_r($output);


Output (for your sample data)



Array
(
[0] => Array
(
[id] => 41844
[regNumber] => CSM-057.05
[name] => 100n SMD 0603 10% X7R 25V
[madeBy2] => Array
(
"Pro_Sinoskysun International (HK) CO., LTD",
"Pro_RUTRONIK Elektronische Ba",
"Pro_TTI",
"Pro_ARROW CENTRAL EUROPE GMBH",
"Pro_TDK",
"Pro_YANGZHIDING ELECTRONICS"
)
)
[1] => Array
(
[id] => 41301
[regNumber] => P+CSM-302.01
[name] => 100n SMD 0603 5% X7R 25V
[madeBy2] =>
)
[2] => Array
(
[id] => 24526
[regNumber] => P+CSM-097.04
[name] => 100n SMD 0603 10% X7R 25V
[madeBy2] => Pro_TDK
)
)


Demo on 3v4l.org






share|improve this answer


























  • That's it.. Output is not what you wrote but i can work with it.. :) Thanks a lot for help. Output pastebin.com/NB1bCGD2

    – Daniel Sympho Weiss
    Nov 16 '18 at 7:50













  • @DanielSymphoWeiss You're right... pasting output from 3v4l is a pain so I copied yours. :-P but I've added an alternate solution which will only convert madeBy2 into an array if there are multiple values so it does give you that output.

    – Nick
    Nov 16 '18 at 7:55











  • one more question.. If i want to add description to row in array madeBy2 how can i do it?

    – Daniel Sympho Weiss
    Nov 19 '18 at 8:03











  • @DanielSymphoWeiss I can't see where description is coming from? It might be better if you asked a new question using this answer as a starting point and adding the additional information. On the upside you can gain additional rep by accepting another answer!

    – Nick
    Nov 19 '18 at 8:09











  • ah okay :) i wont spam so better to ask here :D gimme sec

    – Daniel Sympho Weiss
    Nov 19 '18 at 8:11



















0














I build up an array which is indexed by the id value from the data, if it's not already set then I create a new element with the data - although madeBy2 is created as an array. If the element already exists, then it adds the new value to the existing list of values ( using )...



$out = ;
foreach ( $inp as $data ) {
if ( isset ($out[$data['id']])) {
$out[$data['id']]["madeBy2"] = $data["madeBy2"];
}
else {
$out[$data['id']] = [ "id"=>$data['id'],
"regNumber" => $data['regNumber'],
"name" => $data['name'],
"madeBy2" => [$data['madeBy2']]];
}
}
print_r(array_values($out));


This will leave an array indexed by the id so I use array_values() in the print_r(), you can leave it in if you need it.






share|improve this answer































    0














    Comments in code



    foreach ($array as $arr) {
    if(!isset($output[$arr["id"]])){ // if item does not exist
    $output[$arr["id"]] = $arr; // create array
    $output[$arr["id"]]['madeBy2'] = [$output[$arr["id"]]['madeBy2']]; // change type on madeBy2 to array
    }else{
    $output[$arr["id"]]['madeBy2'] = $arr['madeBy2']; // it item exist add madeBy2 to the list
    }
    }
    $output = array_values($output); // remove associative array keys.
    print_r($output);


    This method does not rely on the list being sorted and only madeBy2 is hardcode.

    Meaning if one array has an extra item this method will keep it opposed to Nigel's.

    Output:



    Array
    (
    [0] => Array
    (
    [id] => 41844
    [regNumber] => CSM-057.05
    [name] => 100nSMD060310%X7R25V
    [madeBy2] => Array
    (
    [0] => Pro_SinoskysunInternational(HK) CO.,LTD
    [1] => Pro_RUTRONIKElektronischeBa
    [2] => Pro_TTI
    [3] => Pro_ARROWCENTRALEUROPEGMBH
    [4] => Pro_TDK
    [5] => Pro_YANGZHIDINGELECTRONICS
    )

    )

    [1] => Array
    (
    [id] => 41301
    [regNumber] => P+CSM-302.01
    [name] => 100n SMD 0603 5% X7R 25V
    [madeBy2] => Array
    (
    [0] =>
    )

    )

    [2] => Array
    (
    [id] => 24526
    [regNumber] => P+CSM-097.04
    [name] => 100n SMD 0603 10% X7R 25V
    [madeBy2] => Array
    (
    [0] => Pro_TDK
    )

    )

    )


    https://3v4l.org/iCjk1



    Demo with extra item:
    https://3v4l.org/0jmpL






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53333032%2farray-merge-to-multidimensional-by-key%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      This code will produce your desired output, regardless of whether the array has different id values. This code does assume that all similar products are grouped in order though:



      $output = array();
      $i = -1;
      foreach ($array as $arr) {
      if ($i < 0 || $output[$i]['id'] != $arr['id'] || $output[$i]['regNumber'] != $arr['regNumber'] || $output[$i]['name'] != $arr['name']) {
      // new product
      $output[++$i] = $arr;
      $output[$i]['madeBy2'] = array($arr['madeBy2']);
      }
      else {
      // same product, new manufacturer
      $output[$i]['madeBy2'] = $arr['madeBy2'];
      }
      }
      print_r($output);


      Demo on 3v4l.org



      Alternate version Only makes madeBy2 element into an array if there are multiple values:



      $output = array();
      $i = -1;
      foreach ($array as $arr) {
      if ($i < 0 || $output[$i]['id'] != $arr['id'] || $output[$i]['regNumber'] != $arr['regNumber'] || $output[$i]['name'] != $arr['name']) {
      // new product
      $output[++$i] = $arr;
      }
      else {
      // same product, new manufacturer
      if (!is_array($output[$i]['madeBy2']))
      $output[$i]['madeBy2'] = array($output[$i]['madeBy2'], $arr['madeBy2']);
      else
      $output[$i]['madeBy2'] = $arr['madeBy2'];
      }
      }
      print_r($output);


      Output (for your sample data)



      Array
      (
      [0] => Array
      (
      [id] => 41844
      [regNumber] => CSM-057.05
      [name] => 100n SMD 0603 10% X7R 25V
      [madeBy2] => Array
      (
      "Pro_Sinoskysun International (HK) CO., LTD",
      "Pro_RUTRONIK Elektronische Ba",
      "Pro_TTI",
      "Pro_ARROW CENTRAL EUROPE GMBH",
      "Pro_TDK",
      "Pro_YANGZHIDING ELECTRONICS"
      )
      )
      [1] => Array
      (
      [id] => 41301
      [regNumber] => P+CSM-302.01
      [name] => 100n SMD 0603 5% X7R 25V
      [madeBy2] =>
      )
      [2] => Array
      (
      [id] => 24526
      [regNumber] => P+CSM-097.04
      [name] => 100n SMD 0603 10% X7R 25V
      [madeBy2] => Pro_TDK
      )
      )


      Demo on 3v4l.org






      share|improve this answer


























      • That's it.. Output is not what you wrote but i can work with it.. :) Thanks a lot for help. Output pastebin.com/NB1bCGD2

        – Daniel Sympho Weiss
        Nov 16 '18 at 7:50













      • @DanielSymphoWeiss You're right... pasting output from 3v4l is a pain so I copied yours. :-P but I've added an alternate solution which will only convert madeBy2 into an array if there are multiple values so it does give you that output.

        – Nick
        Nov 16 '18 at 7:55











      • one more question.. If i want to add description to row in array madeBy2 how can i do it?

        – Daniel Sympho Weiss
        Nov 19 '18 at 8:03











      • @DanielSymphoWeiss I can't see where description is coming from? It might be better if you asked a new question using this answer as a starting point and adding the additional information. On the upside you can gain additional rep by accepting another answer!

        – Nick
        Nov 19 '18 at 8:09











      • ah okay :) i wont spam so better to ask here :D gimme sec

        – Daniel Sympho Weiss
        Nov 19 '18 at 8:11
















      1














      This code will produce your desired output, regardless of whether the array has different id values. This code does assume that all similar products are grouped in order though:



      $output = array();
      $i = -1;
      foreach ($array as $arr) {
      if ($i < 0 || $output[$i]['id'] != $arr['id'] || $output[$i]['regNumber'] != $arr['regNumber'] || $output[$i]['name'] != $arr['name']) {
      // new product
      $output[++$i] = $arr;
      $output[$i]['madeBy2'] = array($arr['madeBy2']);
      }
      else {
      // same product, new manufacturer
      $output[$i]['madeBy2'] = $arr['madeBy2'];
      }
      }
      print_r($output);


      Demo on 3v4l.org



      Alternate version Only makes madeBy2 element into an array if there are multiple values:



      $output = array();
      $i = -1;
      foreach ($array as $arr) {
      if ($i < 0 || $output[$i]['id'] != $arr['id'] || $output[$i]['regNumber'] != $arr['regNumber'] || $output[$i]['name'] != $arr['name']) {
      // new product
      $output[++$i] = $arr;
      }
      else {
      // same product, new manufacturer
      if (!is_array($output[$i]['madeBy2']))
      $output[$i]['madeBy2'] = array($output[$i]['madeBy2'], $arr['madeBy2']);
      else
      $output[$i]['madeBy2'] = $arr['madeBy2'];
      }
      }
      print_r($output);


      Output (for your sample data)



      Array
      (
      [0] => Array
      (
      [id] => 41844
      [regNumber] => CSM-057.05
      [name] => 100n SMD 0603 10% X7R 25V
      [madeBy2] => Array
      (
      "Pro_Sinoskysun International (HK) CO., LTD",
      "Pro_RUTRONIK Elektronische Ba",
      "Pro_TTI",
      "Pro_ARROW CENTRAL EUROPE GMBH",
      "Pro_TDK",
      "Pro_YANGZHIDING ELECTRONICS"
      )
      )
      [1] => Array
      (
      [id] => 41301
      [regNumber] => P+CSM-302.01
      [name] => 100n SMD 0603 5% X7R 25V
      [madeBy2] =>
      )
      [2] => Array
      (
      [id] => 24526
      [regNumber] => P+CSM-097.04
      [name] => 100n SMD 0603 10% X7R 25V
      [madeBy2] => Pro_TDK
      )
      )


      Demo on 3v4l.org






      share|improve this answer


























      • That's it.. Output is not what you wrote but i can work with it.. :) Thanks a lot for help. Output pastebin.com/NB1bCGD2

        – Daniel Sympho Weiss
        Nov 16 '18 at 7:50













      • @DanielSymphoWeiss You're right... pasting output from 3v4l is a pain so I copied yours. :-P but I've added an alternate solution which will only convert madeBy2 into an array if there are multiple values so it does give you that output.

        – Nick
        Nov 16 '18 at 7:55











      • one more question.. If i want to add description to row in array madeBy2 how can i do it?

        – Daniel Sympho Weiss
        Nov 19 '18 at 8:03











      • @DanielSymphoWeiss I can't see where description is coming from? It might be better if you asked a new question using this answer as a starting point and adding the additional information. On the upside you can gain additional rep by accepting another answer!

        – Nick
        Nov 19 '18 at 8:09











      • ah okay :) i wont spam so better to ask here :D gimme sec

        – Daniel Sympho Weiss
        Nov 19 '18 at 8:11














      1












      1








      1







      This code will produce your desired output, regardless of whether the array has different id values. This code does assume that all similar products are grouped in order though:



      $output = array();
      $i = -1;
      foreach ($array as $arr) {
      if ($i < 0 || $output[$i]['id'] != $arr['id'] || $output[$i]['regNumber'] != $arr['regNumber'] || $output[$i]['name'] != $arr['name']) {
      // new product
      $output[++$i] = $arr;
      $output[$i]['madeBy2'] = array($arr['madeBy2']);
      }
      else {
      // same product, new manufacturer
      $output[$i]['madeBy2'] = $arr['madeBy2'];
      }
      }
      print_r($output);


      Demo on 3v4l.org



      Alternate version Only makes madeBy2 element into an array if there are multiple values:



      $output = array();
      $i = -1;
      foreach ($array as $arr) {
      if ($i < 0 || $output[$i]['id'] != $arr['id'] || $output[$i]['regNumber'] != $arr['regNumber'] || $output[$i]['name'] != $arr['name']) {
      // new product
      $output[++$i] = $arr;
      }
      else {
      // same product, new manufacturer
      if (!is_array($output[$i]['madeBy2']))
      $output[$i]['madeBy2'] = array($output[$i]['madeBy2'], $arr['madeBy2']);
      else
      $output[$i]['madeBy2'] = $arr['madeBy2'];
      }
      }
      print_r($output);


      Output (for your sample data)



      Array
      (
      [0] => Array
      (
      [id] => 41844
      [regNumber] => CSM-057.05
      [name] => 100n SMD 0603 10% X7R 25V
      [madeBy2] => Array
      (
      "Pro_Sinoskysun International (HK) CO., LTD",
      "Pro_RUTRONIK Elektronische Ba",
      "Pro_TTI",
      "Pro_ARROW CENTRAL EUROPE GMBH",
      "Pro_TDK",
      "Pro_YANGZHIDING ELECTRONICS"
      )
      )
      [1] => Array
      (
      [id] => 41301
      [regNumber] => P+CSM-302.01
      [name] => 100n SMD 0603 5% X7R 25V
      [madeBy2] =>
      )
      [2] => Array
      (
      [id] => 24526
      [regNumber] => P+CSM-097.04
      [name] => 100n SMD 0603 10% X7R 25V
      [madeBy2] => Pro_TDK
      )
      )


      Demo on 3v4l.org






      share|improve this answer















      This code will produce your desired output, regardless of whether the array has different id values. This code does assume that all similar products are grouped in order though:



      $output = array();
      $i = -1;
      foreach ($array as $arr) {
      if ($i < 0 || $output[$i]['id'] != $arr['id'] || $output[$i]['regNumber'] != $arr['regNumber'] || $output[$i]['name'] != $arr['name']) {
      // new product
      $output[++$i] = $arr;
      $output[$i]['madeBy2'] = array($arr['madeBy2']);
      }
      else {
      // same product, new manufacturer
      $output[$i]['madeBy2'] = $arr['madeBy2'];
      }
      }
      print_r($output);


      Demo on 3v4l.org



      Alternate version Only makes madeBy2 element into an array if there are multiple values:



      $output = array();
      $i = -1;
      foreach ($array as $arr) {
      if ($i < 0 || $output[$i]['id'] != $arr['id'] || $output[$i]['regNumber'] != $arr['regNumber'] || $output[$i]['name'] != $arr['name']) {
      // new product
      $output[++$i] = $arr;
      }
      else {
      // same product, new manufacturer
      if (!is_array($output[$i]['madeBy2']))
      $output[$i]['madeBy2'] = array($output[$i]['madeBy2'], $arr['madeBy2']);
      else
      $output[$i]['madeBy2'] = $arr['madeBy2'];
      }
      }
      print_r($output);


      Output (for your sample data)



      Array
      (
      [0] => Array
      (
      [id] => 41844
      [regNumber] => CSM-057.05
      [name] => 100n SMD 0603 10% X7R 25V
      [madeBy2] => Array
      (
      "Pro_Sinoskysun International (HK) CO., LTD",
      "Pro_RUTRONIK Elektronische Ba",
      "Pro_TTI",
      "Pro_ARROW CENTRAL EUROPE GMBH",
      "Pro_TDK",
      "Pro_YANGZHIDING ELECTRONICS"
      )
      )
      [1] => Array
      (
      [id] => 41301
      [regNumber] => P+CSM-302.01
      [name] => 100n SMD 0603 5% X7R 25V
      [madeBy2] =>
      )
      [2] => Array
      (
      [id] => 24526
      [regNumber] => P+CSM-097.04
      [name] => 100n SMD 0603 10% X7R 25V
      [madeBy2] => Pro_TDK
      )
      )


      Demo on 3v4l.org







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 16 '18 at 7:54

























      answered Nov 16 '18 at 7:46









      NickNick

      37k132343




      37k132343













      • That's it.. Output is not what you wrote but i can work with it.. :) Thanks a lot for help. Output pastebin.com/NB1bCGD2

        – Daniel Sympho Weiss
        Nov 16 '18 at 7:50













      • @DanielSymphoWeiss You're right... pasting output from 3v4l is a pain so I copied yours. :-P but I've added an alternate solution which will only convert madeBy2 into an array if there are multiple values so it does give you that output.

        – Nick
        Nov 16 '18 at 7:55











      • one more question.. If i want to add description to row in array madeBy2 how can i do it?

        – Daniel Sympho Weiss
        Nov 19 '18 at 8:03











      • @DanielSymphoWeiss I can't see where description is coming from? It might be better if you asked a new question using this answer as a starting point and adding the additional information. On the upside you can gain additional rep by accepting another answer!

        – Nick
        Nov 19 '18 at 8:09











      • ah okay :) i wont spam so better to ask here :D gimme sec

        – Daniel Sympho Weiss
        Nov 19 '18 at 8:11



















      • That's it.. Output is not what you wrote but i can work with it.. :) Thanks a lot for help. Output pastebin.com/NB1bCGD2

        – Daniel Sympho Weiss
        Nov 16 '18 at 7:50













      • @DanielSymphoWeiss You're right... pasting output from 3v4l is a pain so I copied yours. :-P but I've added an alternate solution which will only convert madeBy2 into an array if there are multiple values so it does give you that output.

        – Nick
        Nov 16 '18 at 7:55











      • one more question.. If i want to add description to row in array madeBy2 how can i do it?

        – Daniel Sympho Weiss
        Nov 19 '18 at 8:03











      • @DanielSymphoWeiss I can't see where description is coming from? It might be better if you asked a new question using this answer as a starting point and adding the additional information. On the upside you can gain additional rep by accepting another answer!

        – Nick
        Nov 19 '18 at 8:09











      • ah okay :) i wont spam so better to ask here :D gimme sec

        – Daniel Sympho Weiss
        Nov 19 '18 at 8:11

















      That's it.. Output is not what you wrote but i can work with it.. :) Thanks a lot for help. Output pastebin.com/NB1bCGD2

      – Daniel Sympho Weiss
      Nov 16 '18 at 7:50







      That's it.. Output is not what you wrote but i can work with it.. :) Thanks a lot for help. Output pastebin.com/NB1bCGD2

      – Daniel Sympho Weiss
      Nov 16 '18 at 7:50















      @DanielSymphoWeiss You're right... pasting output from 3v4l is a pain so I copied yours. :-P but I've added an alternate solution which will only convert madeBy2 into an array if there are multiple values so it does give you that output.

      – Nick
      Nov 16 '18 at 7:55





      @DanielSymphoWeiss You're right... pasting output from 3v4l is a pain so I copied yours. :-P but I've added an alternate solution which will only convert madeBy2 into an array if there are multiple values so it does give you that output.

      – Nick
      Nov 16 '18 at 7:55













      one more question.. If i want to add description to row in array madeBy2 how can i do it?

      – Daniel Sympho Weiss
      Nov 19 '18 at 8:03





      one more question.. If i want to add description to row in array madeBy2 how can i do it?

      – Daniel Sympho Weiss
      Nov 19 '18 at 8:03













      @DanielSymphoWeiss I can't see where description is coming from? It might be better if you asked a new question using this answer as a starting point and adding the additional information. On the upside you can gain additional rep by accepting another answer!

      – Nick
      Nov 19 '18 at 8:09





      @DanielSymphoWeiss I can't see where description is coming from? It might be better if you asked a new question using this answer as a starting point and adding the additional information. On the upside you can gain additional rep by accepting another answer!

      – Nick
      Nov 19 '18 at 8:09













      ah okay :) i wont spam so better to ask here :D gimme sec

      – Daniel Sympho Weiss
      Nov 19 '18 at 8:11





      ah okay :) i wont spam so better to ask here :D gimme sec

      – Daniel Sympho Weiss
      Nov 19 '18 at 8:11













      0














      I build up an array which is indexed by the id value from the data, if it's not already set then I create a new element with the data - although madeBy2 is created as an array. If the element already exists, then it adds the new value to the existing list of values ( using )...



      $out = ;
      foreach ( $inp as $data ) {
      if ( isset ($out[$data['id']])) {
      $out[$data['id']]["madeBy2"] = $data["madeBy2"];
      }
      else {
      $out[$data['id']] = [ "id"=>$data['id'],
      "regNumber" => $data['regNumber'],
      "name" => $data['name'],
      "madeBy2" => [$data['madeBy2']]];
      }
      }
      print_r(array_values($out));


      This will leave an array indexed by the id so I use array_values() in the print_r(), you can leave it in if you need it.






      share|improve this answer




























        0














        I build up an array which is indexed by the id value from the data, if it's not already set then I create a new element with the data - although madeBy2 is created as an array. If the element already exists, then it adds the new value to the existing list of values ( using )...



        $out = ;
        foreach ( $inp as $data ) {
        if ( isset ($out[$data['id']])) {
        $out[$data['id']]["madeBy2"] = $data["madeBy2"];
        }
        else {
        $out[$data['id']] = [ "id"=>$data['id'],
        "regNumber" => $data['regNumber'],
        "name" => $data['name'],
        "madeBy2" => [$data['madeBy2']]];
        }
        }
        print_r(array_values($out));


        This will leave an array indexed by the id so I use array_values() in the print_r(), you can leave it in if you need it.






        share|improve this answer


























          0












          0








          0







          I build up an array which is indexed by the id value from the data, if it's not already set then I create a new element with the data - although madeBy2 is created as an array. If the element already exists, then it adds the new value to the existing list of values ( using )...



          $out = ;
          foreach ( $inp as $data ) {
          if ( isset ($out[$data['id']])) {
          $out[$data['id']]["madeBy2"] = $data["madeBy2"];
          }
          else {
          $out[$data['id']] = [ "id"=>$data['id'],
          "regNumber" => $data['regNumber'],
          "name" => $data['name'],
          "madeBy2" => [$data['madeBy2']]];
          }
          }
          print_r(array_values($out));


          This will leave an array indexed by the id so I use array_values() in the print_r(), you can leave it in if you need it.






          share|improve this answer













          I build up an array which is indexed by the id value from the data, if it's not already set then I create a new element with the data - although madeBy2 is created as an array. If the element already exists, then it adds the new value to the existing list of values ( using )...



          $out = ;
          foreach ( $inp as $data ) {
          if ( isset ($out[$data['id']])) {
          $out[$data['id']]["madeBy2"] = $data["madeBy2"];
          }
          else {
          $out[$data['id']] = [ "id"=>$data['id'],
          "regNumber" => $data['regNumber'],
          "name" => $data['name'],
          "madeBy2" => [$data['madeBy2']]];
          }
          }
          print_r(array_values($out));


          This will leave an array indexed by the id so I use array_values() in the print_r(), you can leave it in if you need it.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 16 '18 at 7:46









          Nigel RenNigel Ren

          28.3k62034




          28.3k62034























              0














              Comments in code



              foreach ($array as $arr) {
              if(!isset($output[$arr["id"]])){ // if item does not exist
              $output[$arr["id"]] = $arr; // create array
              $output[$arr["id"]]['madeBy2'] = [$output[$arr["id"]]['madeBy2']]; // change type on madeBy2 to array
              }else{
              $output[$arr["id"]]['madeBy2'] = $arr['madeBy2']; // it item exist add madeBy2 to the list
              }
              }
              $output = array_values($output); // remove associative array keys.
              print_r($output);


              This method does not rely on the list being sorted and only madeBy2 is hardcode.

              Meaning if one array has an extra item this method will keep it opposed to Nigel's.

              Output:



              Array
              (
              [0] => Array
              (
              [id] => 41844
              [regNumber] => CSM-057.05
              [name] => 100nSMD060310%X7R25V
              [madeBy2] => Array
              (
              [0] => Pro_SinoskysunInternational(HK) CO.,LTD
              [1] => Pro_RUTRONIKElektronischeBa
              [2] => Pro_TTI
              [3] => Pro_ARROWCENTRALEUROPEGMBH
              [4] => Pro_TDK
              [5] => Pro_YANGZHIDINGELECTRONICS
              )

              )

              [1] => Array
              (
              [id] => 41301
              [regNumber] => P+CSM-302.01
              [name] => 100n SMD 0603 5% X7R 25V
              [madeBy2] => Array
              (
              [0] =>
              )

              )

              [2] => Array
              (
              [id] => 24526
              [regNumber] => P+CSM-097.04
              [name] => 100n SMD 0603 10% X7R 25V
              [madeBy2] => Array
              (
              [0] => Pro_TDK
              )

              )

              )


              https://3v4l.org/iCjk1



              Demo with extra item:
              https://3v4l.org/0jmpL






              share|improve this answer




























                0














                Comments in code



                foreach ($array as $arr) {
                if(!isset($output[$arr["id"]])){ // if item does not exist
                $output[$arr["id"]] = $arr; // create array
                $output[$arr["id"]]['madeBy2'] = [$output[$arr["id"]]['madeBy2']]; // change type on madeBy2 to array
                }else{
                $output[$arr["id"]]['madeBy2'] = $arr['madeBy2']; // it item exist add madeBy2 to the list
                }
                }
                $output = array_values($output); // remove associative array keys.
                print_r($output);


                This method does not rely on the list being sorted and only madeBy2 is hardcode.

                Meaning if one array has an extra item this method will keep it opposed to Nigel's.

                Output:



                Array
                (
                [0] => Array
                (
                [id] => 41844
                [regNumber] => CSM-057.05
                [name] => 100nSMD060310%X7R25V
                [madeBy2] => Array
                (
                [0] => Pro_SinoskysunInternational(HK) CO.,LTD
                [1] => Pro_RUTRONIKElektronischeBa
                [2] => Pro_TTI
                [3] => Pro_ARROWCENTRALEUROPEGMBH
                [4] => Pro_TDK
                [5] => Pro_YANGZHIDINGELECTRONICS
                )

                )

                [1] => Array
                (
                [id] => 41301
                [regNumber] => P+CSM-302.01
                [name] => 100n SMD 0603 5% X7R 25V
                [madeBy2] => Array
                (
                [0] =>
                )

                )

                [2] => Array
                (
                [id] => 24526
                [regNumber] => P+CSM-097.04
                [name] => 100n SMD 0603 10% X7R 25V
                [madeBy2] => Array
                (
                [0] => Pro_TDK
                )

                )

                )


                https://3v4l.org/iCjk1



                Demo with extra item:
                https://3v4l.org/0jmpL






                share|improve this answer


























                  0












                  0








                  0







                  Comments in code



                  foreach ($array as $arr) {
                  if(!isset($output[$arr["id"]])){ // if item does not exist
                  $output[$arr["id"]] = $arr; // create array
                  $output[$arr["id"]]['madeBy2'] = [$output[$arr["id"]]['madeBy2']]; // change type on madeBy2 to array
                  }else{
                  $output[$arr["id"]]['madeBy2'] = $arr['madeBy2']; // it item exist add madeBy2 to the list
                  }
                  }
                  $output = array_values($output); // remove associative array keys.
                  print_r($output);


                  This method does not rely on the list being sorted and only madeBy2 is hardcode.

                  Meaning if one array has an extra item this method will keep it opposed to Nigel's.

                  Output:



                  Array
                  (
                  [0] => Array
                  (
                  [id] => 41844
                  [regNumber] => CSM-057.05
                  [name] => 100nSMD060310%X7R25V
                  [madeBy2] => Array
                  (
                  [0] => Pro_SinoskysunInternational(HK) CO.,LTD
                  [1] => Pro_RUTRONIKElektronischeBa
                  [2] => Pro_TTI
                  [3] => Pro_ARROWCENTRALEUROPEGMBH
                  [4] => Pro_TDK
                  [5] => Pro_YANGZHIDINGELECTRONICS
                  )

                  )

                  [1] => Array
                  (
                  [id] => 41301
                  [regNumber] => P+CSM-302.01
                  [name] => 100n SMD 0603 5% X7R 25V
                  [madeBy2] => Array
                  (
                  [0] =>
                  )

                  )

                  [2] => Array
                  (
                  [id] => 24526
                  [regNumber] => P+CSM-097.04
                  [name] => 100n SMD 0603 10% X7R 25V
                  [madeBy2] => Array
                  (
                  [0] => Pro_TDK
                  )

                  )

                  )


                  https://3v4l.org/iCjk1



                  Demo with extra item:
                  https://3v4l.org/0jmpL






                  share|improve this answer













                  Comments in code



                  foreach ($array as $arr) {
                  if(!isset($output[$arr["id"]])){ // if item does not exist
                  $output[$arr["id"]] = $arr; // create array
                  $output[$arr["id"]]['madeBy2'] = [$output[$arr["id"]]['madeBy2']]; // change type on madeBy2 to array
                  }else{
                  $output[$arr["id"]]['madeBy2'] = $arr['madeBy2']; // it item exist add madeBy2 to the list
                  }
                  }
                  $output = array_values($output); // remove associative array keys.
                  print_r($output);


                  This method does not rely on the list being sorted and only madeBy2 is hardcode.

                  Meaning if one array has an extra item this method will keep it opposed to Nigel's.

                  Output:



                  Array
                  (
                  [0] => Array
                  (
                  [id] => 41844
                  [regNumber] => CSM-057.05
                  [name] => 100nSMD060310%X7R25V
                  [madeBy2] => Array
                  (
                  [0] => Pro_SinoskysunInternational(HK) CO.,LTD
                  [1] => Pro_RUTRONIKElektronischeBa
                  [2] => Pro_TTI
                  [3] => Pro_ARROWCENTRALEUROPEGMBH
                  [4] => Pro_TDK
                  [5] => Pro_YANGZHIDINGELECTRONICS
                  )

                  )

                  [1] => Array
                  (
                  [id] => 41301
                  [regNumber] => P+CSM-302.01
                  [name] => 100n SMD 0603 5% X7R 25V
                  [madeBy2] => Array
                  (
                  [0] =>
                  )

                  )

                  [2] => Array
                  (
                  [id] => 24526
                  [regNumber] => P+CSM-097.04
                  [name] => 100n SMD 0603 10% X7R 25V
                  [madeBy2] => Array
                  (
                  [0] => Pro_TDK
                  )

                  )

                  )


                  https://3v4l.org/iCjk1



                  Demo with extra item:
                  https://3v4l.org/0jmpL







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 16 '18 at 10:36









                  AndreasAndreas

                  16.9k41844




                  16.9k41844






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53333032%2farray-merge-to-multidimensional-by-key%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Xamarin.iOS Cant Deploy on Iphone

                      Glorious Revolution

                      Dulmage-Mendelsohn matrix decomposition in Python