1.array_walk ($arr, $func, [$data])
Use a user-defined function to traverse all elements and return True/false
$func is the name of a function
By default, the value of the first $arr of the two parameters is passed, the second $arr is subscript, and a pair of the two passes OH
1<?PHP2 $arr=Array(' A ', ' B ', ' C ');3 Array_walk($arr, ' Test ');//here the second parameter, test, is the name of a function .4 functionTest (&$val,$key)//here the first argument plus the reference, then modifying the $val is equivalent to modifying the elements in the $arr5 {6 $val= ' X_ '.$val;//and give him a prefix here .7 }8 Print_r($arr);//output result Array (' x_a ', ' x_b ', ' x_c ');9 //If you change the $arr to $arr = Array (' A ', ' B ', ' C ', array (1, 2, 3)), the printed result will be an array (' x_a ', ' x_b ', ' x_c ', ' X_array ');Ten?>
If there is a third parameter passed in, the third parameter is passed to the third parameter in the function defined by the second parameter
1<?PHP2 Array_walk($arr, ' Test ', ' x_ ');3 functionTest (&$val,$key,$prefix){4 $val=$prefix.$val;//actually, the $prefix here is the x_.5 }6 Print_r($arr);//The output is the same as the above array (' x_a ', ' x_b ', ' x_c ');7?>
2. How to iterate through arrays: each and List
1 //method One:2 $sports=Array(3' Football ' = ' good ',4' Swimming ' = ' very well ',5' Running ' = ' not good ');6 while($elem= each($sports)) {7 Echo $elem[' Key ']. ":".$elem[' Value ']. " <br/> ";8 }9 Ten //Method Two: One $sports=Array( A' Football ' = ' good ', -' Swimming ' = ' very well ', -' Running ' = ' not good '); the while(List($key,$value) = each($sports)) { - Echo $key.": ".$value." <br/> "; - } - //Football:good + //Swimming:very well - //Running:not good +
Use of PHP array functions