note 56897 modified in function.array-walk by danbrown

From: Date: Mon, 22 Aug 2011 13:10:07 +0000
Subject: note 56897 modified in function.array-walk by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-182639@lists.php.net to get a copy of this message
It can be very useful to pass the third (optional) parameter by reference while modifying it permanently in callback function. This will cause passing modified parameter to next iteration of array_walk(). The exaple below enumerates items in the array: <?php function enumerate( &$item1, $key, &$startNum ) { $item1 = $startNum++ ." $item1"; } $num = 1; $fruits = array( "lemon", "orange", "banana", "apple"); array_walk($fruits, 'enumerate', $num ); print_r( $fruits ); echo '$num is: '. $num ."\n"; ?> This outputs: Array ( [0] => 1 lemon [1] => 2 orange [2] => 3 banana [3] => 4 apple ) $num is: 1 Notice at the last line of output that outside of array_walk() the $num parameter has initial value of 1. This is because array_walk() does not take the third parameter by reference.. so what if we pass the reference as the optional parameter.. <?php $num = 1; $fruits = array( "lemon", "orange", "banana", "apple"); array_walk($fruits, 'enumerate', &$num ); // reference here print_r( $fruits ); echo '$num is: '. $num ."\n"; echo "we've got ". ($num - 1) ." fruits in the basket!"; ?> This outputs: Array ( [0] => 1 lemon [1] => 2 orange [2] => 3 banana [3] => 4 apple ) $num is: 5 we've got 4 fruits in the basket! Now $num has changed so we are able to count the items (without calling count() unnecessarily). As a conclusion, using references with array_walk() can be powerful toy but this should be done carefully since modifying third parameter outside the array_walk() is not always what we want. --was-- It can be very useful to pass the third (optional) parameter by reference while modifying it permanently in callback function. This will cause passing modified parameter to next iteration of array_walk(). The exaple below enumerates items in the array: <? function enumerate( &$item1, $key, &$startNum ) { $item1 = $startNum++ ." $item1"; } $num = 1; $fruits = array( "lemon", "orange", "banana", "apple"); array_walk($fruits, 'enumerate', $num ); print_r( $fruits ); echo '$num is: '. $num ."\n"; ?> This outputs: Array ( [0] => 1 lemon [1] => 2 orange [2] => 3 banana [3] => 4 apple ) $num is: 1 Notice at the last line of output that outside of array_walk() the $num parameter has initial value of 1. This is because array_walk() does not take the third parameter by reference.. so what if we pass the reference as the optional parameter.. <? $num = 1; $fruits = array( "lemon", "orange", "banana", "apple"); array_walk($fruits, 'enumerate', &$num ); // reference here print_r( $fruits ); echo '$num is: '. $num ."\n"; echo "we've got ". ($num - 1) ." fruits in the basket!"; ?> This outputs: Array ( [0] => 1 lemon [1] => 2 orange [2] => 3 banana [3] => 4 apple ) $num is: 5 we've got 4 fruits in the basket! Now $num has changed so we are able to count the items (without calling count() unnecessarily). As a conclusion, using references with array_walk() can be powerful toy but this should be done carefully since modifying third parameter outside the array_walk() is not always what we want. http://php.net/manual/en/function.array-walk.php

« previous php.notes (#182639) next »