& sign before an array object...why?
Posted: Tue Jan 08, 2008 1:23 pm
hi, i was just reading about the array_walk function...the example states a clever piece:
what i don't understand is the test_alter function....why must there be an ampersan (&) before $item1, and why is $key present in the parameters...it is not used in the function. I ask all this, as i have applied a similar function, but i want to understand why it works.
thanks
Code: Select all
<?php
$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
function test_alter(&$item1, $key, $prefix)
{
$item1 = "$prefix: $item1";
}
function test_print($item2, $key)
{
echo "$key. $item2<br />\n";
}
echo "Before ...:\n";
array_walk($fruits, 'test_print');
array_walk($fruits, 'test_alter', 'fruit');
echo "... and after:\n";
array_walk($fruits, 'test_print');
?>thanks