A summary of how PHP iterates through an array

Source: Internet
Author: User

Today, a friend of mine asked me a question. PHP traverses the array method and tells her a few. By the way, write a summary of the article, if the summary is not fully also asked friends to point out

First, foreach ()

foreach () is the simplest and most efficient way to traverse the data in an array.

?

1 2 3 4 5 6 <?php $urls = Array (' AAA ', ' BBB ', ' CCC ', ' ddd '); foreach ($urls as $url) {echo "This Site URL is $url! <br/> "; }?>

Show Results:

?

1 2 3 4 This site URL is AAA This site URL is bbb This site URL is CCC this site URL is ddd

The second, while () and list (), each () is used together.

?

1 2 3 4 5 6 <?php $urls = Array (' AAA ', ' BBB ', ' CCC ', ' ddd '); while (the list ($key, $val) = each ($urls)) {echo ' This Site URL is $val. <br/> ';}?>

Show Results:

?

1 2 3 4 This site URL is AAA This site URL is bbb This site URL is CCC this site URL is ddd

Third, for () use for traversal array

?

1 2 3 4 5 6 7 <?php $urls = Array (' AAA ', ' BBB ', ' CCC ', ' ddd '); for ($i = 0; $i < count ($urls); $i + +) {$str = $urls [$i]; echo "This Site URL is $str. <br/>";}?>

Show Results:

?

1 2 3 4 This site URL is AAA This site URL is bbb This site URL is CCC this site URL is ddd

Sometimes some people are asking these kinds of methods to iterate through an array. Which is quicker, and here's a simple test to see

=========== below to test the speed of three kinds of traversal arrays ===========

Generally, there are three ways to traverse an array, for, while, foreach. One of the simplest and most convenient is foreach. Let's take a moment to test how long it takes to traverse a one-dimensional array with 50,000 subscripts.

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 The <?php $arr = array (); for" ($i =. for 0; $i < 50000; $i + +) {$arr []= $i *rand (1000,9999);} function GetRuntime () {list ($usec, $sec) =explode ("", Microtime ()); Return ((float) $usec + (float) $sec); ###################################### $time _start= getruntime (); for ($i = 0; $i < count ($arr); $i + +) {$str = $arr [$i];} $time _end= getruntime (); $time _used= $time _end-$time _start; Echo ' Used time of the for: '. Round ($time _used, 7). ' (s) <br/><br/> '; Unset ($str, $time _start, $time _end, $time _used); ###################################### $time _start= getruntime (); while (the list ($key, $val) = each ($arr)) {$str = $val;} $time _end= getruntime (); $time _used= $time _end-$time _start; Echo ' Used time of: '. Round ($time _used, 7). ' (s) <br/><br/> '; Unset ($str, $key, $val, $time _stArt, $time _end, $time _used); ###################################### $time _start= getruntime (); foreach ($arr as$key=> $val) {$str = $val;} $time _end= getruntime (); $time _used= $time _end-$time _start; Echo ' Used time of foreach: '. Round ($time _used, 7). ' (s) <br/><br/> ';?>

Test results:

?

1 2 3 Used time of for:0.0228429 (s) Used time of while:0.0544658 (s) Used time of foreach:0.0085628 (s)

After repeated tests, the results show that foreach is the fastest and slowest while traversing the same array. In principle, foreach operates on an array copy (by copying the arrays) while the while is manipulated by moving the internal index of the array, which is generally logically considered to be faster than foreach (because foreach first copies the array when it starts executing). While the while directly moving internal indicators. ), but the result is just the opposite. The reason for this is that foreach is the internal implementation of PHP while the while is the universal loop structure. So, in a common application, foreach is simple and highly efficient. Under PHP5, foreach can also traverse the properties of a class.

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.