3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php $array_1=array("1a-dogs","2a-cats","3a-birds","4a-people"); $array_2=array("4a-walking","2a-cats"); function getKeyStartingWith($array, $startVal){ foreach($array as $key => $val){ if(strpos($val, $startVal) === 0){ return $key; } } return false; } function getMergedArray($array_1, $array_2){ $array_3 = array(); foreach($array_1 as $key => $val){ $startVal = substr($val, 0, 2); $array_2_key = getKeyStartingWith($array_2, $startVal); if($array_2_key !== false){ $array_3[$key] = $array_2[$array_2_key]; } else { $array_3[$key] = $val; } } return $array_3; } $array_1 = getMergedArray($array_1, $array_2); print_r($array_1);
Output for 4.3.0 - 5.6.28, hhvm-3.10.0, 7.0.0 - 7.1.0
Array ( [0] => 1a-dogs [1] => 2a-cats [2] => 3a-birds [3] => 4a-walking )
Output for hhvm-3.12.0 - 3.13.2

Process exited with code 137.