Appending One Array to Another : array_merge « Data Structure « PHP






Appending One Array to Another

 
<?php
  function array_display($array, $pre=FALSE){
    $tag = $pre ? 'pre' : 'p';
    printf("<%s>%s</%s>\n", $tag, var_export($array, TRUE), $tag);
  }

  $arr1 = array(1, 2, 3);
  $arr2 = array(10, 20, 30);
  $arr3 = array(5, 10, 15, 20);
  
  $comb1 = array_merge($arr1, $arr2);
  $comb2 = array_merge($arr2, $arr1);
  $comb3 = array_merge($arr3, $arr2, $arr1);
  
  array_display($comb1);
  array_display($comb2);
  array_display($comb3);
?>

<?php
 
  $arr4 = array(10 => 'a', 11 => 'b', 12 => 'c');
  
  array_display(array_merge($arr1, $arr4), TRUE);
  array_display($arr1 + $arr4, TRUE);
?>

<?php
  $arr5 = array(1 => 'x', 2 => 'y', 3 => 'z');
  
  array_display(array_merge($arr1, $arr5), TRUE);
  array_display($arr1 + $arr5, TRUE);
?>

<?php
  $dogs1 = array( 'L' => 'C', 'B' => 'S','R' => 'A');

  $dogs2 = array('R' => 'D', 'T' => 'S');

  array_display(array_merge($dogs1, $dogs2), TRUE);
  array_display($dogs1 + $dogs2, TRUE);
?>
  
  








Related examples in the same category

1.Using array_merge() and the + operator with associative arrays as well
2.array_merge
3.array_merge( ) function combines two or more arrays by renumbering numerical indexes and overwriting string indexes
4.array_merge( ) retains array keys when possible.
5.array_merge() and + operator
6.array_merge() example
7.array_merge() function merges 1 to N arrays together
8.obtaining the union of two arrays