function Merge($Left, $Right) { $Result = array(); while(count($Left) > 0 && count($Right) > 0) { if($Left[0] <= $Right[0]) { $Result[] = $Left[0]; array_shift($Left); } else { $Result[] = $Right[0]; array_shift($Right); } } if(count($Left) > 0) { foreach($Left as $v) { $Result[] = $v; } } if(count($Right) > 0) { foreach($Right as $v) { $Result[] = $v; } } return $Result; }