Get the App
SLTechnology News&Howtos  ›  Development  › 

What is PHP array traversal

Shulou Source: shulou.com Published: 2022-06-02 19:39:36 09月10日 Update

This article mainly explains "what is PHP array traversal", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "what is PHP array traversal" bar!

1. What is array traversal

In fact, it is a process of accessing each content in the array, in the process of traversal, we can get the content we need according to the conditions, or manipulate the elements in the array.

For, foreach, each () and list () are commonly used in PHP programming to traverse the array.

2. For loop traversal array

Basic format:

For loops through the array without changing the internal pointer of the array.

3. Foreach traversal array

Basic format:

Traverses the array, using current () to output the value of the current cell pointed to by the array pointer

The output of running the above program in PHP 7 is:

Int (0) int (0) int (0)

In versions prior to PHP 7, it would output:

Int (1) int (2) bool (false)

This shows that versions prior to PHP 7 changed the pointers inside the array when doing foreach loops.

When iterating through an array using foreach, foreach operates on the backup of the array, and modifying the array inside the loop does not affect access to the array outside the loop. Examples are as follows:

The output is as follows:

Array

(

[0] = > 0

[1] = > 1

[2] = > 2

)

If it is a reference loop, the changes made to the array inside the loop will affect the array itself. Examples are as follows:

The running results are as follows:

Array

(

[0] = > 0

[1] = > 2

[2] = > 4

) 3. Each () function

The each () function returns the current key value in the array and moves the array pointer forward. After each () is executed, the array pointer stays at the next element in the array or at the end of the array. If you want to traverse the array with each again, you must use reset ().

The running results are as follows:

Array

(

[1] = > my name

[value] = > my name

[0] = > 0

[key] = > 0

)

His name is Zhang San

Array

(

[1] = > called Zhang San

[value] = > is called Zhang San

[0] = > 1

[key] = > 1

)

La ~

4. List () function

Use list () to assign the values of the array to variables, as shown in the following example:

The running results are as follows:

My name is Zhang Sanla ~ 24 years old Zhang San

The combination of the each () function and the list () function can traverse the array, as shown in the following example:

The running results are as follows:

A = > my name

B = > called Zhang San

C = > la ~

D = > 24 years old

E = > Zhang San thank you for reading, the above is the content of "what is PHP array traversal", after the study of this article, I believe you have a deeper understanding of what PHP array traversal is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Array Zhang San loop name pointer result element function run content example output learning that is point format version procedure influence move Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux NVidia Apple Shulou Information Huawei