Php learning traversal array

Source: Internet
Author: User
Traversing all elements in an array is a common operation. you can perform queries or other operations during the traversal process. In php learning, there are many ways to traverse arrays. The most common method is to traverse arrays through the foreach () and list () functions.

Traversing all elements in an array is a common operation. you can perform queries or other operations during the traversal process. In php learning, there are many ways to traverse arrays. The most common method is to traverse arrays through the foreach () and list () functions.
1. foreach traverses the array
For example:
$ Arr1 = array ("team" => "barcelona", "name" => "messi", "age" => 24 );
Foreach ($ arr1 as $ value ){
Echo $ value ."
";
}
In the preceding example, php executes a loop for each element of the array $ arr1, assigns $ value to each element, and processes each element in the internal order of the array.
2. use the list () function to traverse arrays.
The list () function assigns values in the array to some variables. Similar to the array () function, it is not a real function, but a language structure. The list () function can only be used as an array of numeric indexes, and the number index starts from 0.
To better understand the list () function, we use the list () function and the each () function to obtain the user login information stored in the array.
For example:
While (list ($ name, $ value) = each ($ _ POST )){
If ($ name! = "Real "){
Echo "$ name: $ value
";
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.