_php tutorial for iterating through array methods in PHP

Source: Internet
Author: User
There are several implementations of traversing an array:

1. An array of contiguous indexes:

The traversal of a contiguous array is simple, because the index of the array is continuous (0 1 2 ...), so we can first use the count () function to calculate the number of elements in the arrays, and then create a for loop, as follows:

$subject =array ("Maths", "中文版", "Physics");

$num _elements=count ($subject); At this point the value of $num_elements is 3

for ($i =0; $i < $num _elements; + + $i) {

Echo ("$subject [$i]

n ");

}

The result is:

Maths

中文版

Physics

Here we give $i an initial value of 0, because the index of the array element defaults to 0 1 2, if the first element's index is not 0, we just need:

$subject =array (3=> "maths", "中文版", "Physics");

$num _elements=count ($subject) +3; Watch this.

for ($i =3; $i < $num _elements; + + $i) {

Echo ("$subject [$i]

n ");

}

Here the $i is assigned a value of 3, while $num_elements=count ($subject) +3, which is easily overlooked.

2. An array of non-contiguous indexes:

$subject =array ("M" = "maths", "e" = "English", "P" and "Physics");

An array is created here, with the index values "M", "E" "P", respectively, how do the arrays traverse?

Method One: Use the list () and each () function combination

$subject =array ("M" = "maths", "e" = "English", "P" and "Physics");

Reset ($subject); Reset the pointer to the first element

while (list ($key, $value) =each ($subject)) {

echo "$key is $value

n "; }

Method Two: Using the foreach statement

$subject =array ("M" = "maths", "e" = "English", "P" and "Physics");

foreach ($subject as $key = = $value) {

echo "$key is $value

n ";

}

?>

is not simpler than list () and each ().

The difference between the foreach and list () each () combination:

foreach performs operations on a copy of the original array. His advantage is that it does not affect the position of the current array pointer, and the disadvantage is that it takes a long time to replicate for a large array.

The list () each () combination is obviously used to traverse, and then the position of the pointer changes.

Method Three: Use the Array_walk () function to iterate over an array

Array_walk () is a user-defined function that handles every element in the array.

$subject =array ("Maths", "中文版", "Physics");

function Printelement ($element) {

Print ("$element

n ");

}

Array_walk ($subject, "printelement");

The above is to help the home network for you to introduce the traversal array method in PHP, I hope to help you.

http://www.bkjia.com/PHPjc/371793.html www.bkjia.com true http://www.bkjia.com/PHPjc/371793.html techarticle There are several ways to iterate through an array: 1, an array of contiguous indexes, it is simple to implement a continuous array, because the index of the array is continuous (0 1 2), so we can start with count ...

  • 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.