PHP iterate through the array of methods Summary, PHP all over the summary _php tutorial

Source: Internet
Author: User

PHP iterate through the array of methods Summary, PHP all over the summary


A friend of mine asked me a question today. PHP iterates through the array, telling her a few. By the way, write a summary of the article, if the summary is not complete also ask friends to point out

First, foreach ()

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

<?php     $urls = Array (' AAA ', ' BBB ', ' CCC ', ' ddd ');     foreach ($urls as $url) {       
";

Show Results:

Second, while () and list (), each () is used together.

<?php     $urls = Array (' AAA ', ' BBB ', ' CCC ', ' ddd ');     while (list ($key, $val) = each ($urls)) {       echo ' This Site URL is $val.
";

Show Results:

Third, for () use for traversal array

<?php     $urls = Array (' AAA ', ' BBB ', ' CCC ', ' ddd ');     for ($i = 0; $i < count ($urls), $i + +) {       $str = $urls [$i];       echo "This Site URL is $str.
";

Show Results:

Sometimes someone is asking these kinds of ways to iterate over an array which is quicker, and the following is a simple test to understand
=========== below to test the speed of three traversal arrays ===========
In general, there are three ways to traverse an array, for, while, foreach. One of the simplest and most convenient is foreach. Let's test the time spent together traversing a one-dimensional array with 50,000 subscripts.

<?php $arr = Array ();   for ($i = 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 For: '. Round ($time _used, 7). ' (s)

'; Unset ($str, $time _start, $time _end, $time _used); ###################################### $time _start= getruntime (); while (list ($key, $val) = each ($arr)) {$str = $val; } $time _end= getruntime (); $time _used= $time _end-$time _start; Echo ' used time of while: '. Round ($time _used, 7). ' (s)

'; 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)

'; ?>

Test results:

After repeated tests, the result shows that for traversing the same array, the foreach speed is the fastest and the slowest is the while. In principle, foreach operates on an array copy (by copying the arrays), while the while is manipulated by moving the internal indicators of the array, which is generally considered to be faster than foreach (since foreach first copies the array in the beginning of execution). While the while moves the internal indicator directly. ), but the result is just the opposite. The reason should be that foreach is an internal implementation of PHP, while the while is a common loop structure. Therefore, foreach is simple and efficient in common applications. Under PHP5, foreach can also traverse the properties of a class.

The above mentioned is the whole content of this article, I hope you can like.

http://www.bkjia.com/PHPjc/992546.html www.bkjia.com true http://www.bkjia.com/PHPjc/992546.html techarticle PHP iterate through the array of methods Summary, PHP all over the summary today, a friend asked me a question PHP traversal array method, told her a few. By the way write a summary of the article, if the summary is not complete ...

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