Home  >  Article  >  Backend Development  >  How to use array_walk_recursive function in PHP to apply callback function to multi-dimensional array elements

How to use array_walk_recursive function in PHP to apply callback function to multi-dimensional array elements

WBOY
WBOYOriginal
2023-06-26 11:46:23519browse

In PHP, the array_walk_recursive function can be used to apply a callback function to all elements in a multidimensional array. This article will introduce how to use the array_walk_recursive function.

  1. What is a multidimensional array

In PHP, arrays can be multidimensional, that is, one array can contain another array. For example, the following array is a two-dimensional array:

$array = array(
    array('name' => 'john', 'age' => 20),
    array('name' => 'mary', 'age' => 25)
);
  1. array_walk_recursive function

array_walk_recursive function is used to apply a callback function to all elements in a multi-dimensional array. Its syntax is as follows:

array_walk_recursive ( array &$array , callable $callback [, mixed $userdata = NULL ] ) : bool

Parameter description:

  • $array: the multidimensional array to be processed
  • $callback: the callback function applied to the array elements
  • $userdata: Optional user data, passed to the callback function

Returns true if successful, otherwise returns false.

  1. Using the array_walk_recursive function

The following is an example of using the array_walk_recursive function. Suppose we have a multidimensional array containing the names and ages of users, and we want to add 10 to each age and print out each user's name and new age.

function add_age(&$item, $key) {
    if ($key == 'age') {
        $item += 10;
    }
}

$array = array(
    array('name' => 'john', 'age' => 20),
    array('name' => 'mary', 'age' => 25)
);

array_walk_recursive($array, 'add_age');

foreach ($array as $key => $value) {
    echo $value['name'] . ' ' . $value['age'] . '
'; }

In this example, we define a callback function add_age, which adds 10 to all ages. We then pass this callback function to the array_walk_recursive function, passing it our multidimensional array. Finally, we use a foreach loop to print out each user's name and new age.

  1. Summary

The array_walk_recursive function can easily apply a callback function to all elements in a multidimensional array. It is very useful when dealing with multi-level nested arrays, which can avoid writing a lot of repeated code. We can use this function to complete various operations, such as data filtering, conversion, etc.

The above is the detailed content of How to use array_walk_recursive function in PHP to apply callback function to multi-dimensional array elements. 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