Skip to content

Instantly share code, notes, and snippets.

@michaeluno
Last active December 13, 2015 22:39
Show Gist options
  • Save michaeluno/4985656 to your computer and use it in GitHub Desktop.
Save michaeluno/4985656 to your computer and use it in GitHub Desktop.
array_merge() is useful to merge arrays but it is confusing which parameter takes precedence. Also it is different from the method with the plus operator. ( e.g. $arr1 + $arr2 ). It could be more like join two array elements. So it may be used to append or pre-pend numerically indexed arrays.
<?php
$array1 = array( 'a', 'b', 'c' );
$array2 = array( 'd', 'e', );
$result1 = array_merge( $array2, $array1 );
$result2 = array_merge( $array1, $array2 );
echo '<pre>' . print_r( $result1, true ) . '</pre>';
echo '<pre>' . print_r( $result2, true ) . '</pre>';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment