2

I need to merge two arrays wich have the following format:

array(9) 
{ 
[0]=> array(1) { ["BLA"]=> string(7) "bis 050" } 
[1]=> array(1) { ["BLA"]=> string(7) "bis 060" } 
[2]=> array(1) { ["BLA"]=> string(7) "bis 070" } 
[3]=> array(1) { ["BLA"]=> string(7) "bis 080" } 
[4]=> array(1) { ["BLA"]=> string(7) "bis 090" } 
[5]=> array(1) { ["BLA"]=> string(7) "bis 100" } 
[6]=> array(1) { ["BLA"]=> string(7) "bis 110" } 
[7]=> array(1) { ["BLA"]=> string(7) "bis 120" } 
[8]=> array(1) { ["BLA"]=> string(6) "gr 120" } 
} 

array(5) 
{ 
[0]=> array(2) {
   ["BLA"]=> string(7) "bis 050" 
   ["Amount"]=> string(3) "832" } 
[1]=> array(2) { 
   ["BLA"]=> string(7) "bis 060" 
   ["Amount"]=> string(3) "448" } 
[2]=> array(2) { 
   ["BLA"]=> string(7) "bis 090" 
   ["Amount"]=> string(4) "1216" } 
[3]=> array(2) { 
   ["BLA"]=> string(7) "bis 100" 
   ["Amount"]=> string(4) "1024" } 
[4]=> array(2) { 
   ["BLA"]=> string(7) "bis 110" 
   ["Amount"]=> string(3) "896" } 
}

I tried array_merge() and array_merge_recursive() but it does not work. My goal is to write the second key and its value from array2 (Amount) into array 1 where the value for the first key (BLA) is identical. In addition I would like to write "Amount":"0", if there is no corresponding value in array2. Is there any way to do this with php?

The result should look like the following:

    Result:
{ 
[0]=> array(2) { 
   ["BLA"]=> string(7) "bis 050"  
   ["Amount"]=> string(3) "832" } 
[1]=> array(2) { 
   ["BLA"]=> string(7) "bis 060"  
   ["Amount"]=> string(3) "448" } 
[2]=> array(2) { 
   ["BLA"]=> string(7) "bis 070" 
   ["Amount"]=> string(1) "0" }  
[3]=> array(2) { 
   ["BLA"]=> string(7) "bis 080" 
   ["Amount"]=> string(1) "0" }  
[4]=> array(2) { 
   ["BLA"]=> string(7) "bis 090" 
   ["Amount"]=> string(4) "1216" }
[5]=> array(2) { 
   ["BLA"]=> string(7) "bis 100" 
   ["Amount"]=> string(4) "1024" }  
[6]=> array(2) { 
   ["BLA"]=> string(7) "bis 110" 
   ["Amount"]=> string(3) "896" } 
[7]=> array(2) { 
   ["BLA"]=> string(7) "bis 120" 
   ["Amount"]=> string(1) "0" }  
[8]=> array(2) { 
   ["BLA"]=> string(6) "gr 120" 
   ["Amount"]=> string(1) "0" } 
}   
Daniel
  • 23
  • 4
  • 2
    array keys need to be unique –  Feb 14 '16 at 21:01
  • http://stackoverflow.com/a/4769240/1604068 this will probably help you – Sevvlor Feb 14 '16 at 21:01
  • It is not only a merging, the default structure is changed. What's happened where a key does not exit in the other array? – C Würtz Feb 14 '16 at 21:14
  • @Dagon they are unique... – fusion3k Feb 14 '16 at 21:18
  • I agree with you Dagon, but the way it's laid out appears that the keys are unique, but the primary index for the array is a hidden numerical one. IE: '0 => array("BLA" => "bis 050", "Amoun...... – Hiphop03199 Feb 14 '16 at 21:19
  • I think you should refine your code, because repeating the keys "bla" wouldn't help anyone give you a good answer; like what key needs to be merged with what value? Follow Dagon's recommendation above – Cedric Ipkiss Feb 14 '16 at 21:27
  • I changed the layout, so ist better to see, that the keys are unique. The Key "BLA" exits in every array and there are no more/other values for "BLA" than in the first array. – Daniel Feb 14 '16 at 21:34
  • It's the third of fourth question I saw last 12 days.. :-/ .. I have unaccepted answer [here](http://stackoverflow.com/questions/35303697/how-the-right-way-to-combine-the-two-arrays/35304109#35304109) ... It's very similar question – Wizard Feb 14 '16 at 21:43

2 Answers2

0

I put together a quick demo which seems to solve your problem. Let me know if you need any additional help with it.

<?php

$arrOne = array(
    array("BLA" => "bis 050"),
    array("BLA" => "bis 060"),
    array("BLA" => "bis 070"),
    array("BLA" => "bis 080"),
    array("BLA" => "bis 090"),
    array("BLA" => "bis 100"),
    array("BLA" => "bis 110"),
    array("BLA" => "bis 120"),
    array("BLA" => "gr 120")
);

$arrTwo = array(
    array("BLA" => "bis 050","Amount" => "832"),
    array("BLA" => "bis 060","Amount" => "448"),
    array("BLA" => "bis 090","Amount" => "1216"),
    array("BLA" => "bis 100","Amount" => "1024"),
    array("BLA" => "bis 110","Amount" => "896")
);


$arrOutput = array();

foreach($arrOne as $arrOneValue) {
    $searchKey = $arrOneValue["BLA"];

    foreach($arrTwo as $arrTwoValue) {
        if($arrTwoValue["BLA"] == $searchKey) {
            $arrOutput[] = array("BLA" => $searchKey, "Amount" => $arrTwoValue["Amount"]);
            continue 2; // Continue the outer loop
        }
    }

    // We didn't find the key
    $arrOutput[] = array("BLA" => $searchKey, "Amount" => "0");
}

var_dump($arrOutput);
?>

Which produces something like the following:

array(9) { 
    [0]=> array(2) { ["BLA"]=> string(7) "bis 050" ["Amount"]=> string(3) "832" } 
    [1]=> array(2) { ["BLA"]=> string(7) "bis 060" ["Amount"]=> string(3) "448" } 
    [2]=> array(2) { ["BLA"]=> string(7) "bis 070" ["Amount"]=> string(1) "0" } 
    [3]=> array(2) { ["BLA"]=> string(7) "bis 080" ["Amount"]=> string(1) "0" } 
    [4]=> array(2) { ["BLA"]=> string(7) "bis 090" ["Amount"]=> string(4) "1216" } 
    [5]=> array(2) { ["BLA"]=> string(7) "bis 100" ["Amount"]=> string(4) "1024" } 
    [6]=> array(2) { ["BLA"]=> string(7) "bis 110" ["Amount"]=> string(3) "896" } 
    [7]=> array(2) { ["BLA"]=> string(7) "bis 120" ["Amount"]=> string(1) "0" } 
    [8]=> array(2) { ["BLA"]=> string(6) "gr 120" ["Amount"]=> string(1) "0" } 
} 
Hiphop03199
  • 729
  • 3
  • 16
  • Thanks. In general, I'd suggest you consider rearranging the arrays you have so you can leverage some of the nice array merging/general functions PHP supports. The simple option would be to drop the 'BLA' key and have the "bis ###" parts as the keys themselves. This is entirely dependent on what you're dealing with here though, so I'll leave that up to you. – Hiphop03199 Feb 14 '16 at 22:02
0

Try this:

$array1 = '[
{"BLA":"bis 050"},
{"BLA":"bis 060"},
{"BLA":"bis 070"},
{"BLA":"bis 080"},
{"BLA":"bis 090"},
{"BLA":"bis 100"},
{"BLA":"bis 110"},
{"BLA":"bis 120"},
{"BLA":"gr 120"}
]';

$array2 = '[
{"BLA":"bis 050","Amount":"832"},
{"BLA":"bis 060","Amount":"448"},
{"BLA":"bis 090","Amount":"1216"},
{"BLA":"bis 100","Amount":"1024"},
{"BLA":"bis 110","Amount":"896"}
]';

$a1 = json_decode($array1, TRUE);
$a2 = json_decode($array2, TRUE);
$result = [];

foreach ($a1 as $source) {
  $found = FALSE;
  foreach ($a2 as $key => $content) {
    if ($content['BLA'] == $source['BLA']) {
      $element = $content;
      $a2[$key]['BLA'] = NULL; // ensure not to reuse furtherly
      $found = TRUE;
      break;
    }
  }
  if (!$found) {
    $element = ['BLA' => $source['BLA'], 'Amount' => 0];
  }
  $result[] = $element;
}
// add unused $a2 content, if any
if ($a2) {
  $result += $a2;
}

echo '<pre>' . print_r(str_replace(",{", ",\n{", json_encode($result)), TRUE) . '</pre>';
cFreed
  • 4,404
  • 1
  • 23
  • 33