The function of the list function is to assign it an array variable, and it will assign the elements with integers as key values in the array to its own parameters in order from small to large key values. If the parameters are not enough, the parameters will be filled. , if there are not enough values in the array, the parameter is assigned a null value, and the code continues
The values in the $lst array variable, the key values are integers are 1=>'a' in the front and 0=>'one in the back. The advantage of the list function is that even if the key value is small, the ranking is The subsequent elements will also be assigned to the front-to-back parameters in the list function in order from small to large.
Since each does not loop through the array, it only moves the pointer each time it is used, and the return value is false at the end of the array, so it is most appropriate to put it in a while
$arr = array('one'=>'a','two'=>'b', 'three'=>'c');
while(list($key, $val) = each($arr)){
echo $key.' => '.$val.'
';
}
4. Use the array internal pointer movement function
The internal pointer of the array points to the first element in the array by default. The functions are roughly as follows: current(): returns the element value where the current pointer points to the position in the array; key(): returns the element key where the current pointer points to the position in the array; next(): moves the pointer to the next element position; prev(): moves the pointer to the previous element position; reset(): moves the array pointer to the position of the first element of the array; end(): moves the pointer to the first element position of the array The array pointer moves to the position of the last element of the array. The parameters they act on are the array variables themselves, and combined with do...while, they can achieve sequential and reverse order traversal of the array.
Copy code
echo 'key:'.key($arr).' current:'.current($arr).'
'; //The current key and value point to the first element of the array by default
next($arr); //Move back one element and point to the second element
echo 'key:'.key($arr).' current:'.current($arr).'
'; //Current key and value
next($arr); //Move back one more point to the third element
echo 'key:'.key($arr).' current:'.current($arr).'
'; //Current key and value
prev($arr); //Move forward one, pointing to the second element
echo 'key:'.key($arr).' current:'.current($arr).'
'; //Current key and value
end($arr); //Move to the last element of the array
echo 'key:'.key($arr).' current:'.current($arr).'
'; //Current key and value
reset($arr); //Move to the first element of the array
echo 'key:'.key($arr).' current:'.current($arr).'
'; //Current key and value
http://www.bkjia.com/PHPjc/893562.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/893562.htmlTechArticlePHP array traversal array is a very powerful weapon in PHP. It is convenient and easy to use. Extremely flexible, you can use it to implement linked lists, stacks, and queues in data structures...
Statement of this Website
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 admin@php.cn