So, I'm not sure whether you want array_merge(); sort();
or a 'merge the values in the order of the key' kind of answer, so here's both.
First the simple, 'merge -> sort' example
$array1 = array('1', '5', '3');
$array2 = array('2', '4', '6');
// Perform a standard merge
$array = array_merge ($array1, $array2);
// $array looks like this:
// $array = ['1', '5', '3', '2', '4', '6'];
// So sort it.
sort($array);
// And we get the following, note it will lose the key order.
// $array = ['1', '2', '3', '4', '5', '6'];
If you want to merge the arrays in 'key -> array number' order then you could do something like this:
function merge($array1, $array2) {
$arrays = func_get_args();
$lengths = array();
$output = array();
// First find the longest array.
foreach ($arrays as $key => $array) {
// Remove the keys.
$arrays[$key] = array_values($array);
// Find the length.
$lengths[] = count($array);
}
// Get the dimensions.
$length = max($lengths);
$count = count($arrays);
// For each element (of the longest array)
for ($x = 0; $x < $length; $x++)
// For each array
for ($y = 0; $y < $count; $y++)
// If the value exists
if (isset($arrays[$y][$x]))
// Add it to the output
$output[] = $arrays[$y][$x];
return $output;
}
$array1 = array('1', '5', '3');
$array2 = array('2', '4', '6');
$array = merge($array1, $array2);
/* Output:
Array
(
[0] => 1
[1] => 2
[2] => 5
[3] => 4
[4] => 3
[5] => 6
)
*/
$array3 = array('a', 't', 'u', 'i', 'w');
$array = merge($array1, $array2, $array3);
/* Output:
Array
(
[0] => 1
[1] => 2
[2] => a
[3] => 5
[4] => 4
[5] => t
[6] => 3
[7] => 6
[8] => u
[9] => i
[10] => w
)
*/
I will caveat this with "I've not tried this code" but it 'should' work as far as I can see.