PHP Traversal Array Method Summary _php tutorial

Source: Internet
Author: User

Summary of how PHP iterates through arrays


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.

?

1

2

3

4

5

6

$urls = Array (' AAA ', ' BBB ', ' CCC ', ' ddd ');

foreach ($urls as $url) {

echo "This Site URL is $url!
";

}

?>

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

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

?

1

2

3

4

5

6

$urls = Array (' AAA ', ' BBB ', ' CCC ', ' ddd ');

while (list ($key, $val) = each ($urls)) {

echo "This Site URL is $val.
";

}

?>

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

$urls = Array (' AAA ', ' BBB ', ' CCC ', ' ddd ');

for ($i = 0; $i < count ($urls); $i + +) {

$str = $urls [$i];

echo "This Site URL is $str.
";

}

?>

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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

$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:

?

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

http://www.bkjia.com/PHPjc/993431.html www.bkjia.com true http://www.bkjia.com/PHPjc/993431.html techarticle A summary of how PHP iterates through arrays today, a friend of mine asked me a question. The way PHP iterates through the array, tells her a few. By the way to write a summary of the article, if the summary is not complete also ask friends to point out ...

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