Traversal array method in PHP

Source: Internet
Author: User

There are several ways to traverse an array:

1, consecutive index of the array:

It is easy to traverse a contiguous array because the index of the array is contiguous (0 1 2 ...), so we can first compute the number of elements in the group with the Count () function, and then create a for loop, as follows:

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

$num _elements=count ($subject); Then the value of $num_elements is 3

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

Echo ("$subject [$i]

n ");

}

The results are:

Maths

中文版

Physics

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

$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 and $num_elements=count ($subject) +3, which is easily overlooked here.

2, non-continuous index of the array:

$subject =array ("M" => "maths", "E" => "中文版", "P" => "Physics");

An array is created here, the index value is "M" "E" "P", how does such an array traverse?

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

$subject =array ("M" => "maths", "E" => "中文版", "P" => "Physics");

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

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

echo "$key is $value

n "; }

Method Two: Use a foreach statement

$subject =array ("M" => "maths", "E" => "中文版", "P" => "Physics");

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

echo "$key is $value

n ";

}

?>

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

The difference between a foreach and list () each ():

foreach performs an operation on a copy of the original array. His advantage is that it does not affect the position of the current array pointer, but the disadvantage is that for a large array, replication takes a long time.

The list () each () combination is obviously traversed by him, after which the position of the pointer changes.

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

Array_walk () is a user-defined function that handles each element of the array.

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

function Printelement ($element) {

Print ("$element

n ");

}

Array_walk ($subject, "printelement");

The above is the three networking for you to introduce the PHP traversal array method, I hope to help you.

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.