Understanding array merge by index: some questions

I am trying to understand the concept of merge array by index (https://www.w3resource.com/php-exercises/php-array-exercises.php). Are we going to start by minimum index? We can use the following example:
$array1 = array(array(77, 87), array(23, 45));
$array2 = array(“w3resource”, “com”);
Expected Output :
[0] => Array
[0] => w3resource
[1] => 77
[2] => 87
[1] => Array
[0] => com
[1] => 23
[2] => 45
The output shows that we first store the string values in the lowest indexes. But for numerical values we are not considering indexes and starting with larger values first. Kindly explain how we got this output? Is this really a programming concept or is it up to the developer?


This is a concept that the site itself has invented for this exercise.

What they’re doing in their solution is actually two different things, merging and flattening.
The order of the output is their own arbitrary decision; in fact it has nothing to do with the values in the array, they simply said “take parameter1 as the base array, and add to it all of the (1-layer-flattened) values of the other array.”

They then specifically gave their function $array2 as the first parameter, and $array1 as the second. If they’d given it the other way around, the result would have been an array that put the numbers first.
It’s entirely up to the developer when designing custom sort/merging functions as to what the output will be. If the site had been demonstrating PHP’s inbuilt array_merge, we could talk about ‘PHP programming concepts’, but this is a purely custom code exercise.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.