The array_walk() function is a built-in function in PHP. Regardless of the pointer position, the array_walk() function will traverse the entire array and perform specified callback processing on each element of the array; the keys and values of the array elements are parameters in the callback function.
Syntax:
array_walk($array, myFunction, $extraParam)
Parameter description: array_walk() function accepts The following three parameters
●$array: used to specify the input array and pass the target array. This parameter cannot be omitted (required).
● MyFunction: Used to specify the name of the user-defined function. This parameter is also non-omitable (required). User-defined functions usually include two parameters, where the first parameter represents the value of the array, and the second parameter represents the corresponding key.
●$ extraparam: This is an optional parameter and can be omitted; used to specify another extra parameter in addition to the two parameters (array key and value) of the user-defined function.
Return value: array_walk() function returns a Boolean value. Returns TRUE on success and FALSE on failure.
Example 1:
<?php header("content-type:text/html;charset=utf-8"); // 自定义回调函数 function myfunction($value, $key) { echo "键 $key 的值为 $value "."<br>"; } // 定义数组 $arr = array("a"=>"yellow", "b"=>"pink", "c"=>"purple"); // 没有额外参数的调用Array_walk() array_walk($arr, "myfunction"); ?>
Output:
Example 2:
<?php header("content-type:text/html;charset=utf-8"); // 自定义回调函数 function myfunction($value, $key, $extraParam) { echo "$key $extraParam $value "."<br>"; } // 定义数组 $arr = array("green"=>"绿色", "pink"=>"粉红色", "blue"=>"蓝色"); // 有额外参数的调用Array_walk() array_walk($arr, "myfunction", "表示:"); ?>
Output:
##Example 3:
<?php // 自定义回调函数 function myfunction(&$value, $key) { $value = $value + 10; } // 定义函数 $arr = array("first"=>10, "second"=>20, "third"=>30); //没有额外参数的调用Array_walk() array_walk($arr, "myfunction"); // 更新值后输出数组 var_dump($arr); ?>
PHP Tutorial"
The above is the entire content of this article, I hope it will be helpful to everyone's learning. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !The above is the detailed content of How to use array_walk() function in PHP? (code example). For more information, please follow other related articles on the PHP Chinese website!