Written by James McDonald

June 25, 2016

Just a note to self about PHP array_merge

The later array passed into array_merge overwrites the former, also if it’s indexed with numerical keys then the values will just be appended.

<?php

$option_array = [
'element' => 'ooa1',
'optionb' => 'ob1',
'different' => 'diff',
'notin2' => true
];

$oa2 = [
	'element' => 'ooa2',
	'optionb' => 'ob2',
	'different' => 'diff2',
	'notin1' => true
	];


echo "option_array and oa2\n";

var_dump(array_merge($option_array, $oa2));

echo "oa2 and option_array\n";

var_dump(array_merge($oa2, $option_array));

The out put of the above is

jmcd@jmitsmbp01-3 ~ $ php amt.php 
option_array and oa2
array(5) {
  ["element"]=>
  string(4) "ooa2"
  ["optionb"]=>
  string(3) "ob2"
  ["different"]=>
  string(5) "diff2"
  ["notin2"]=>
  bool(true)
  ["notin1"]=>
  bool(true)
}
oa2 and option_array
array(5) {
  ["element"]=>
  string(4) "ooa1"
  ["optionb"]=>
  string(3) "ob1"
  ["different"]=>
  string(4) "diff"
  ["notin1"]=>
  bool(true)
  ["notin2"]=>
  bool(true)
}

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

The reCAPTCHA verification period has expired. Please reload the page.

You May Also Like…