Jump to content

The array_walk() Function with a Closure and Use Variables


iwato

Recommended Posts

Dilemma: In the block of code that follows the $stat_arr1 and $stat_arr2 arrays are identical except for their names. When their elements are compared via the strcmp() function, the $statcmp array should fill with zeros, but it does not. Rather, it fills with a progression of advancing integers.Question: Any ideas as to what is preventing $statcmp from performing its task properly?

$stat_arr1 = array(1,1,1,1);$stat_arr2 = array(1,1,1,1);$statcmp = array();array_walk($stat_arr1, function($key, $value) use ($stat_arr2, &$statcmp) {	list($key_2, $val_2) = each($stat_arr2);	$statcmp[] = strcmp($value, $val_2);	},	$stat_arr2);print_r($statcmp);

Roddy

Link to comment
Share on other sites

Perhaps you have reversed the arguments being passed to your callback function?I wonder if there is any unexpected effect from passing $stat_arr2 in two places like that. I see no advantage to it, and maybe there is no effect at all, but I think I'd experiment with that.

Link to comment
Share on other sites

Perhaps you have reversed the arguments being passed to your callback function?I wonder if there is any unexpected effect from passing $stat_arr2 in two places like that. I see no advantage to it, and maybe there is no effect at all, but I think I'd experiment with that.
The second entry of the $stat_arr2 variable has no effect, but I took it out anyway, as it was redundant.What did make a difference was the reordering of the arguments of the call-back function. For some reason the array_walk() function requires key-value pairs in the call-back function be entered in reverse order. A poorly structured function I should think, but according to manual specifications.<?php $stat_arr1 = array(1,1,1,1); $stat_arr2 = array(1,1,1,1); $statcmp = array(); array_walk($stat_arr1, function($val_1, $key_1) use ($stat_arr2, &$statcmp) { list($key_2, $val_2) = each($stat_arr2); $statcmp[] = strcmp($value, $val_2);} ); print_r($statcmp);?>It now works, as it should.Many thanks, Spartacus!Roddy
Link to comment
Share on other sites

I thought the sequence of arguments was odd too, until I realized that the second argument is optional. I can imagine many situations where the callback only needs to access the values without using the keys.

Link to comment
Share on other sites

I thought the sequence of arguments was odd too, until I realized that the second argument is optional. I can imagine many situations where the callback only needs to access the values without using the keys.
Yes, you are correct, but one more alteration needed to be made that did not surface until I began testing the function with other data strings. Both the $arr2 and $arr_cmp must be referenced, else the each() does not traverse $arr2 properly. The finished product is
<?php	function array_str_cmp($arr1, $arr2) {		$arr_cmp = array();		array_walk($arr1, function($val1) use (&$arr2, &$arr_cmp) {			list($key2, $val2) = each($arr2);			$arr_cmp[] = strcmp($val1, $val2);			});		return $arr_cmp;	}?>

Roddy

Link to comment
Share on other sites

I imagine that each was treating $arr2 as a new array each time, so it was always extracting the first element? Passing it by reference would ensure that the internal pointer stays is advanced as expected.Maybe that was different when $arr2 was passed as the third argument?

Link to comment
Share on other sites

Maybe that was different when $arr2 was passed as the third argument?
The presence or absence of $arr2 as a third argument to the array_walk() function makes no difference. $arr2 must be entered as a referenced use-variable or the function fails. I suspect it has something to do with early binding and the integrity of the closure as an entity unto itself.Roddy
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...