fork(1) download
  1. <?php
  2.  
  3. $a = array(
  4. 0 => 9,
  5. 2 => 13
  6. );
  7.  
  8. $b = array(
  9. 1 => 10,
  10. 2 => 11,
  11. 3 => 12,
  12. 4 => 1
  13. );
  14.  
  15. $ab = mergeArrays($a, $b);
  16. print_r($ab);
  17.  
  18. function mergeArrays($a, $b) {
  19. $adjust = 0;
  20. foreach ($b as $i => $val) {
  21. while (isset($a[$i + $adjust])) {
  22. $adjust++;
  23. }
  24. $a[$i + $adjust] = $val;
  25. }
  26. ksort($a); // Put back in order by indexes
  27. return $a;
  28. }
Success #stdin #stdout 0.01s 82880KB
stdin
Standard input is empty
stdout
Array
(
    [0] => 9
    [1] => 10
    [2] => 13
    [3] => 11
    [4] => 12
    [5] => 1
)