Apply a user function to every member of an array. : array_walk « Data Structure « PHP






Apply a user function to every member of an array.

 
<?
$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";
}
array_walk ($fruits, 'test_print');
reset ($fruits);
array_walk ($fruits, 'test_alter', 'fruit');
reset ($fruits);
array_walk ($fruits, 'test_print');
?>
  
  








Related examples in the same category

1.Applying Functions to Array Elements Using array_walk()
2.array_walk() function applies a function to several or all elements in an array.