NYCPHP Meetup

NYPHP.org

[nycphp-talk] Second time array_walk failed for me

Daniel Kushner nyphp at websapp.com
Thu Nov 6 22:18:20 EST 2003


Phil,

>
> 1) Why does the PHP manual say that "array_walk" CAN change the values in
> the array, when, in fact, it can't?

yes it can!

> 2) If 1 is answerable, does that mean the manual is either misleading on
> array_walk, or, oh no WRONG?

>From the manual:
Note: If function needs to be working with the actual values of the array,
specify the first parameter of function as a reference. Then, any changes
made to those elements will be made in the original array itself.


Some code to test:
<?php
$arr = array('a', 'b', 'c');
var_dump($arr);
function change(&$array, $index) {
	$array = 'I have changed you!';
}

array_walk($arr, 'change');
var_dump($arr);
?>

The output:
array(3) { [0]=> string(1) "a" [1]=> string(1) "b" [2]=> string(1) "c" }

array(3) { [0]=> string(19) "I have changed you!" [1]=> string(19) "I have
changed you!" [2]=> string(19) "I have changed you!" }



Oh my, it works !! Great job PHP Team !

--Daniel





More information about the talk mailing list