In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.