0

I was looking for the use-cases of array_walk(), so I went to the PHP documentation and view this example.

But I don't understand how the function test_alter() modifies the value of the array.

Can you explain me?

Here's the code:

$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');
print_r($fruits);
Rizier123
  • 58,877
  • 16
  • 101
  • 156
  • [`pass by reference`](http://php.net/manual/en/language.references.pass.php) is the keyword here. See the ampersand ->: `function test_alter(` **&** `$item1, $key, $prefix)` ?! – Rizier123 Jun 06 '15 at 13:59
  • 1
    ok i searched about pass by reference and now i understand!thank you! – Dorino Canciani Jun 06 '15 at 14:24

0 Answers0