Home  >  Article  >  Backend Development  >  How to use array_walk() function in PHP? (code example)

How to use array_walk() function in PHP? (code example)

青灯夜游
青灯夜游Original
2019-03-30 14:15:253309browse

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.

How to use array_walk() function in PHP? (code example)

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:

How to use array_walk() function in PHP? (code example)

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:

How to use array_walk() function in PHP? (code example)

##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); 
  
?>

Output:

How to use array_walk() function in PHP? (code example)

Description: By using "&$value" to specify the first parameter in the user-defined function as a reference, it can be changed The value of the array element.

Recommended video tutorials: "

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn