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 positionkey(): Get the index value of the data pointed to by the current pointernext(): Move the internal pointer of the array to the next unitpre( ): 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!