Home > Backend Development > PHP Tutorial > Array traversal in PHP_PHP tutorial

Array traversal in PHP_PHP tutorial

WBOY
Release: 2016-07-13 10:17:17
Original
948 people have browsed it

Array traversal in PHP

Array is a very powerful weapon in PHP. It is convenient and easy to use. Because it is extremely flexible to use, it can be used to realize linked lists, stacks, queues, heaps, so-called dictionaries, sets, etc. in data structures, and can also be converted. into XML format.
1. Use for
The for statement is not a good choice to traverse the array. It is generally not used and has too many limitations, because the subscripts of the array are often not continuous, or there are both integer subscripts and string subscripts, but there is such a If it happens to be an indexed array, and its subscripts are consecutive, then this is also a method.
$array = array('a', 'b', 'c', 'd', 'e');
$size = count($array); //Get the number of array units
for($i=0; $i<$size; $i++)
echo $array[$i].'
';
2. Use foreach
Foreach is more convenient and flexible than for. It is generally used. When using foreach($arr_name as $value), use the as keyword in the previous array to specify its elements. Of course, this is for a one-dimensional array. You can also get the key name of the element, as follows: foreach($arr_name as $key=>$value).
$array = array('os'=>'Linux', 'server'=>'Apache', 'db'=>'mysql', 'language'=>'PHP');
foreach($array as $key=>$value){
echo 'key: '.$key.' --- value: '.$value.'
';
}
:
3. Combining list, each, and while functions
Each time the each function acts on the array, the pointer to the internal element moves backward by one unit. Each time each returns a fixed-format array of key/value pairs, specifically (1=>value, 'value'= >value, 0=>key, 'key'=>key). The next time each is used, it will move to the next element, example
$arr = array('one'=>'a','two'=>'b', 'three'=>'c');
$lst = each($arr);
echo 'each=>
';
var_dump($lst);
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
list($key, $val) = $lst;
echo '
';
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

www.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...
Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template