Home  >  Article  >  Backend Development  >  Detailed explanation of array definition and traversal in PHP

Detailed explanation of array definition and traversal in PHP

小云云
小云云Original
2018-03-29 15:13:441640browse

This article mainly shares with you the detailed explanation of array definition and traversal in PHP. I have studied arrays in the past two days. In fact, it can also be said to be a review. PHP has some similarities with some array applications in other languages.

1. Array definition: ① Directly define $[0]='v' ② Use array to define contact=array['a','b']

2. Array type: ① Associative array ② Index array

3. Array traversal:

1. One-bit array traversal:

① Use For loop to traverse

② Use foreach to traverse:

There are two parameter formats: one One is to directly pass the value of the array to the variable output, the other is to pass the key value and the array value to the variable respectively

③ Use special functions to traverse: each(), list()

each($contact) traverses the elements pointed to by the internal pointer of the current array, moves the pointer backward, and returns false

## when all traversals are completed. #list($var) = $contact Pay the element pointed to by the current array pointer to the variable inside the list

④Use the internal pointer function of the array to traverse the array:

current(): Get the current pointer The content of the position

key(): Get the index value of the data pointed to by the current pointer

next(): Move the internal pointer of the array to the next unit

pre( ): Move the internal pointer of the array back one digit

end(): Move the internal pointer of the array to the last digit

reset(): Unconditionally move the array pointer to the first index position

two. Two-dimensional array traversal:

Two-dimensional array traversal uses for or foreach nesting to traverse

Related recommendations:

Several methods of PHP array definition

The above is the detailed content of Detailed explanation of array definition and traversal in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]