Several common methods for Array traversal in PHP

Source: Internet
Author: User
In the process of encoding, it is no longer common to traverse the specified array. During traversal, many languages use the for loop for traversal, which is convenient and quick. However, in PHP, the subscript of the array is different from that of some languages. In PHP, the subscript of an array can be a string, or a string can be mixed with a number, that is, an associated array. If

In the process of encoding, it is no longer common to traverse the specified array. During traversal, many languages use the for loop for traversal, which is convenient and quick. However, in PHP, the subscript of the array is different from that of some languages. In PHP, the subscript of an array can be a string, or a string can be mixed with a number, that is, an associated array. If

In the process of encoding, it is no longer common to traverse the specified array. During traversal, many languages use the for loop for traversal, which is convenient and quick. However, in PHP, the subscript of the array is different from that of some languages. In PHP, the subscript of an array can be a string, or a string can be mixed with a number, that is, an associated array. If the subscript is a pure number, it is the index array.

1. ()

The for () traversal has a limitation. If it is an associated array, it cannot be traversed according to the increment of the lower mark. If a string suddenly pops up, an error is certainly returned. So in PHP, the range that can be used by for () is the index array.

 "; Print_r ($ arr); echo"
"; // Traverse the array for ($ I = 0; $ I ";}?>2. foreach ()

Foreach () traversal this method in PHP is the best way for both men and women to eat, regardless of whether you are a number or a string, brother does not look at your subscript plus no plus, is to do one by one. It is also very important to be simple and efficient.

  "; Print_r ($ arr); echo"
"; // Traverse the array foreach ($ arr as $ val) {echo $ val ."
";}?>If you need to print a key-value pair, just make a slight modification to foreach.

// Traverse the array foreach ($ arr as $ key => $ val) {echo $ key. ":". $ val ."
";}

3. while (list () = each ())

This method is clever, and the usage frequency is not very high. However, if you encounter it when you look at the code, you must at least understand it. It is better to master it, but it is a little bit of trouble.

  "; Print_r ($ arr); echo"
"; // Traverse the array while (list ($ key, $ val) = each ($ arr) {echo $ key. ':'. $ val ."
";}?>
The code in the preceding example is a one-dimensional array. if you want to traverse an array of two-dimensional, three-dimensional, or higher dimensions, add an if statement continuously. Generally, there are many two-dimensional arrays. I don't think anyone has to write a ten-dimensional array. In that case, when traversing, the taste is not very sour.

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.