I am trying to understand the following code in PHP. Here is the overview,
There are 2 arrays, which consists of key values pairs in the form, $k => $v
These 2 arrays are merged together using array_merge function to form the third array.
Now, this array is passed to a function. Only one argument, the array name is passed.
Here is the code (please note that this code is only a concept, not the real code):
<?php
function test(&$myArray, 0)
{
reset($myArray);
foreach ($myArray as $k => $v)
{
....
}
}
$arr3 = array_merge((array) $arr1, (array) $arr2);
test($arr3)
}
Questions:
- The function is defined in such a way, that it expects two arguments, however we are passing only one argument. Is it because the second argument is always 0 as initialized in the function prototype? So, there is no need to pass the same number of arguments?
- Here the array name is passed. My guess is that, it will pass a pointer to the first element of the array (base address of the array in memory) to the function.
In this case, if you look at the prototype, the array name is preceded with an ampersand. So, this means, a reference to the array, the address?
Why is it necessary to call the reset function on the array. Is it because the array_merge function which was used to form this array was called before the test function? So, it moved the pointer in the array ahead of the first element as a result of merging $arr1 and $arr2?
There is no value returned in the function, test. So, does it modify the value of the original array in the memory itself, and hence there is no need to return an array?
Thanks.
&$myArray), you've got a0in your function parameter list, and you have a mysterious trailing brace.