Comparison of methods and efficiency for retrieving arrays in PHP

Source: Internet
Author: User
This article mainly introduces several methods of PHP looping array list (), each (), and while. This article focuses on the usage of these methods and the efficiency comparison, for your reference. This article mainly introduces several methods of PHP looping array list (), each (), and while. This article focuses on the usage of these methods and the efficiency comparison, for your reference.

Foreach traversal array

When using arrays, we often need to traverse the array and obtain each key or element value. php provides some functions dedicated to traversing the array. This section describes the usage of foreach's array Traversal function.

Structure:

Foreach (array_expression as $ value) statement/* array_expression is the array to be traversed. as is to assign the value of the array to $ value statement, which is a subsequent statement */

Instance 1:

 'Black', 'Red' => 'red', 'green' => 'green', 'yellow' => 'yellow'); foreach ($ color as $ c) echo $ c. "";?>

Using foreach, you can obtain not only the element value, but also the key name. The structure is as follows:

foreach ( array_expression as $key => $value ) statement

Run the following code on the above instance:

foreach( $color as $c) echo $c ."
";

Changed:

foreach( $color as $key => $c) echo $key.$c ."
";

For traversal array

In addition to some predefined php array traversal functions, we can also use the loop feature of the for statement to output the array traversal. The following is an example:

 ";/* Display array */}?>

While (), list (), and each () are used together to traverse arrays.

The syntax format is as follows:

 ";     } ?>

Below we will test the speed of several traversal arrays

Generally, there are three methods to traverse an array: for, while, and foreach. Among them, the easiest and most convenient is foreach. Next, let's test the time it takes to traverse a one-dimensional array with 50000 subtopics.

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

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 for traversing the same array, foreach is the fastest, while is the slowest. In principle, foreach operates on the array copy (by copying the array), while moves the internal indicator of the array, while should be faster than foreach (because foreach first copies the array when starting execution, while directly moves the internal indicator .), But the result is just the opposite. The reason should be that foreach is implemented inside PHP, while is a general loop structure. Therefore, foreach is simple and efficient in common applications. In PHP5, foreach can also traverse class attributes.

I hope this article will 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.