Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Summary of methods for traversing PHP array

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "the method summary of traversing the PHP array", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn the "PHP array traversal method summary" bar!

1. Introduction of three methods of array traversal

1. Foreach ()

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

# example1:

The copy code is as follows:

Display the results:

Do you like red?

Do you like blue?

Do you like green?

Do you like yellow?

2. While ()

While () is usually used in conjunction with list () and each ().

# example2:

The copy code is as follows:

Display the results:

Other list of red.

Other list of blue.

Other list of green.

Other list of yellow.

3. For ()

# example3:

The copy code is as follows:

Display the results:

The number is zero.

The number is one.

The number is two.

2. Introduction of array pointer operation function

Key ()

Mixed key (array input_array)

The key () function returns the key element in the input_array at the current pointer position.

# example4

The copy code is as follows:

Display the results:

Can you name the capitals of these states?

Ohio

Towa

Arizona

Reset ()

Mixed reset (array input_array)

The reset () function is used to set the pointer of input_array back to the beginning of the array. This function is often used if you need to view or process the same array multiple times in a script, and it is also used at the end of a sort.

# example5-append code to # example1

The copy code is as follows:

Display the results:

Do you like red?

Do you like blue?

Do you like green?

Do you like yellow?

0 = > red

1 = > blue

2 = > green

3 = > yellow

Note: assigning an array to another array resets the original array pointer, so in the above example, if we assign $colors to another variable inside the loop, it will cause an infinite loop.

For example, add $S1 = $colors; to the while loop, execute the code again, and the browser will display the results endlessly.

Each ()

Array each (array input_array)

The each () function returns the current key / value pair of the input array and advances the pointer to a position. The returned array contains four keys, keys 0 and key contain key names, and keys 1 and value contain the corresponding data. Returns FALSE if the pointer is at the end of the array before each () is executed.

# example6

The copy code is as follows:

Display the results:

Array ([1] = > Columbus [value] = > Columbus [0] = > Ohio [key] = > Ohio)

Current (), next (), prev (), end ()

Mixed current (array target_array)

The current () function returns the array value at the current pointer position of the target_array array. Unlike the next (), prev (), and end () functions, current () does not move the pointer.

The next () function returns the array value immediately following the next position of the current array pointer.

The prev () function returns the array value at the previous position of the current pointer, or FALSE if the pointer is already at the first position of the array.

The end () function moves the pointer to the last position of the target_array and returns the last element.

# example7

The copy code is as follows:

Display the results:

Apple

Orange

Apple

Banana

Third, test the speed of three kinds of traversing arrays

In general, there are three ways to traverse an array: for, while, and foreach. One of the simplest and most convenient is foreach. Let's first test the time it takes to traverse an one-dimensional array with 50000 subscripts.

Test environment:

Intel Core Due2 2GHz

2GB 1067MHz DDR3

Mac OS X 10.5.7

Apache 2.0.59

MySQL 5.0.41

PHP 5.2.6

# example8

The copy code is as follows:

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 and while is the slowest. In principle, foreach operates on copies of the array (by copying the array), while while operates by moving the internal indicators of the array. Generally speaking, while should be faster than foreach (because foreach copies the array first, while while moves the internal indicators directly. ), but the result is just the opposite The reason should be that foreach is an internal implementation of PHP, while while is a generic loop structure. Therefore, in general applications, foreach is simple and efficient. Under PHP5, foreach can also traverse the properties of a class.

At this point, I believe that everyone on the "PHP array traversal method summary" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report