How to traverse multidimensional array in php

PHPz
Release: 2023-04-19 13:58:44
Original
1090 people have browsed it

PHP, as a commonly used server-side programming language, often deals with multi-dimensional arrays. When parsing and manipulating multidimensional arrays, a common question is how to traverse the various levels of these arrays. This article will introduce how to traverse multi-dimensional arrays in PHP.

  1. foreach loop traverses multi-dimensional arrays

In PHP, we can use foreach loop to traverse the array. When traversing a multi-dimensional array, we can nest foreach loops to access each array level in turn. The following is a simple example:

$array = array( array(1, 2, 3), array(4, 5, 6), array(7, 8, 9) ); foreach ($array as $items) { foreach ($items as $item) { echo $item . ' '; } echo '
'; }
Copy after login

The above code will output the following results:

1 2 3 4 5 6 7 8 9
Copy after login

As you can see, we first use the foreach loop to traverse the first level of the multi-dimensional array, and then continue in the loop body Use a nested foreach loop to traverse the second level. This will output the entire multidimensional array in sequence.

  1. Use recursive functions to traverse multi-dimensional arrays

In addition to using foreach loops, we can also use recursive functions to traverse multi-dimensional arrays. Recursive function refers to a technique that calls the function itself during the definition process of the function. When accessing a multidimensional array, we can write a recursive function that keeps going deeper into each level of the array until it encounters the innermost element. The following is an example of using a recursive function to traverse a multi-dimensional array:

$array = array( 'name' => '张三', 'age' => 18, 'hobbies' => array( 'reading', 'swimming', 'traveling' ), 'friends' => array( array( 'name' => '李四', 'age' => 20 ), array( 'name' => '王五', 'age' => 22, 'hobbies' => array( 'singing', 'dancing' ) ) ) ); function traverseArray($arr) { foreach ($arr as $key => $value) { if (is_array($value)) { traverseArray($value); } else { echo $key . ' => ' . $value . '
'; } } } traverseArray($array);
Copy after login

The above code will output the following results:

name => 张三 age => 18 0 => reading 1 => swimming 2 => traveling name => 李四 age => 20 name => 王五 age => 22 0 => singing 1 => dancing
Copy after login
Copy after login

As you can see, we first define a recursive function named traverseArray, and A foreach loop is used in the function body to traverse the array. Within the loop body, we use the is_array function to detect whether the current element is of array type. If so, call the traverseArray function recursively to continue traversing; otherwise, directly output the key-value pair of the element.

It should be noted that a recursive function may call itself infinitely, resulting in an infinite loop. To avoid this, we need to add a condition to terminate the recursion inside the function body. In the above example, we assume that all non-array elements are the innermost elements, so there is no need to traverse them again.

  1. Use the array_walk_recursive function to traverse multi-dimensional arrays

In PHP, we can also use the array_walk_recursive function to traverse multi-dimensional arrays. This function is a way to recursively execute a callback function on a multi-dimensional array.

The following is an example of using the array_walk_recursive function to traverse a multi-dimensional array:

$array = array( 'name' => '张三', 'age' => 18, 'hobbies' => array( 'reading', 'swimming', 'traveling' ), 'friends' => array( array( 'name' => '李四', 'age' => 20 ), array( 'name' => '王五', 'age' => 22, 'hobbies' => array( 'singing', 'dancing' ) ) ) ); function output($value, $key) { echo $key . ' => ' . $value . '
'; } array_walk_recursive($array, 'output');
Copy after login

The above code will output the following results:

name => 张三 age => 18 0 => reading 1 => swimming 2 => traveling name => 李四 age => 20 name => 王五 age => 22 0 => singing 1 => dancing
Copy after login
Copy after login

We define an output function named output, And pass in the function name in the second parameter of the array_walk_recursive function. In the output function body, we directly output the key-value pair of the current element.

It should be noted that the array_walk_recursive function does not traverse multi-dimensional arrays layer by layer. Instead, it performs a depth-first traversal of each element in the array, recursively traversing each non-array element and then traversing the deepest array element. Therefore, although this function is convenient, it may not meet specific traversal needs in some cases.

Conclusion

In PHP, there are many ways to traverse multi-dimensional arrays, the most common of which is to use foreach loops and recursive functions. Although the array_walk_recursive function also provides an optional traversal method, its effect may not be as flexible as the other two methods. When writing a program, we should choose an appropriate traversal method based on specific needs.

The above is the detailed content of How to traverse multidimensional array in php. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!