0

I have this simple setup:

$array1 = array(0 => array(0 => array('amount' => '49')));
$array2 = array(0 => array(0 => array('amount' => '149')));

$mergetest = array_merge_recursive($array1, $array2);

This outputs:

array(2) {
  [0]=>
  array(1) {
    [0]=>
    array(1) {
      ["amount"]=>
      string(2) "49"
    }
  }
  [1]=>
  array(1) {
    [0]=>
    array(1) {
      ["amount"]=>
      string(3) "149"
    }
  }
}

It should merge in such way, that the output should be like this instead:

    array(1) {
      [0]=>
      array(2) {
        [0]=>
        array(1) {
          ["amount"]=>
          string(2) "49"
        }
        [1]=>
        array(1) {
          ["amount"]=>
          string(3) "149"
        }
      }
    }

Since array index 0 already exists, and so does the array inside it - so it should match by child array instead of parent and in that way add the next child array to it.

Can this be done in a clean way without building a custom function that loops through and checks?

UPDATE

A solution, that also works with:

$array1 = array(0 => array(0 => array('amount' => '49')));
$array2 = array(1243 => array(0 => array('amount' => '49'), 1 => array('amount' => '449')));

Where the output is expected to be in separate parent arrays (0 and 1243), just like the first above output.

3
  • from PHP manual: "If the input arrays have the same string keys": the keys must be string keys (and not numeric indexes) for array_merge_recursive to fuse the rows Commented Oct 25, 2017 at 10:15
  • I think you could merge the 2 inner arrays like you did and then add them to the outer array? e.g. $array1 = array(0 => array('amount' => '49'))); $array2 = array(0 => array('amount' => '149'))); $innermerge = array_merge_recursive($array1, $array2); $mergetest = array(0 => $innermerge); Commented Oct 25, 2017 at 10:16
  • Rather than 1 and a half minimal reproducible examples, it would be better to re-do this question and post one definitive, completed minimal reproducible example to make the question clear and concise. Commented Sep 23, 2022 at 13:36

2 Answers 2

1
<?php
     $array1 = array(0 => array(0 => array('amount' => '49')));
     $array2 = array(0 => array(0 => array('amount' => '149')));

     $result = array_map('array_merge', $array1, $array2);
     var_dump($result);    
?>

And the output will be like the below.

array(1) {
  [0]=>
  array(2) {
    [0]=>
    array(1) {
      ["amount"]=>
      string(2) "49"
    }
    [1]=>
    array(1) {
      ["amount"]=>
      string(3) "149"
    }
  }
}

NOTE : array_merge_recursive() will fail because it appends numeric keys, not merging them:

UPDATE ANSWER FOR ARRAYS WITH DIFFERENT INDEXES

    <?php
        $array1 = array(0 => array(0 => array('amount' => '49')));
        $array2 = array(1243 => array(0 => array('amount' => '49'), 1 => array('amount' => '449')));
        $keys = array_unique(array_merge(array_keys($array1), array_keys($array2)));

        foreach ($keys as $key) {
          if (array_key_exists($key, $array1) && array_key_exists($key, $array2)) {
            $array3[$key] = array_merge($array1[$key], $array2[$key]);
          } elseif (array_key_exists($key, $array1)) {
            $array3[$key] = $array1[$key];
          } else {
            $array3[$key] = $array2[$key];
          }
        }


        var_dump($array3);
?>

And the output will be

array(2) {
  [0]=>
  array(1) {
    [0]=>
    array(1) {
      ["amount"]=>
      string(2) "49"
    }
  }
  [1243]=>
  array(2) {
    [0]=>
    array(1) {
      ["amount"]=>
      string(2) "49"
    }
    [1]=>
    array(1) {
      ["amount"]=>
      string(3) "449"
    }
  }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for this! Could you maybe explain why array_map('array_merge', $..) works over normal array_merge?
I have now tried this, and seems like this doesn't work as expected if the parent index have a different index, it still adds to the same child. I will update with another example.
@Karem I have updated the anwer. Please check whether its working for you.
Great! Works in both use cases! Thanks and I suppose there isn't a default function for this, therefore the need of a foreach.
0

You can get the result you want if you use string keys at the top level rather than integers.

You have two levels of integer-indexed arrays but it seems that to you they have a different nature because you need them to be merged differently. PHP has no way to know that, so with this setup no standard function can do what you need. You will need to change the data structure to mark that difference (e.g. by using string keys on first level and integers on the second) or write a custom merge function here.

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.