An array of PHP traversal

Source: Internet
Author: User
Tags foreach array arrays end key prev reset variable
Arrays in PHP is a very powerful weapon, easy to use, because of the use of unusually flexible, it can be used to implement the data structure of the linked lists, stacks, queues, heaps and so-called dictionaries, sets, etc., can also be converted into XML format.         1, using for       The For statement traversal array is not a good choice, generally not used, the limitations are too large, because the array subscript many times discontinuous, Or there is an integer subscript and a string subscript, but there is a case where the index array, and its subscript is continuous, then this is a method.   <?php     $array = Array (' A ', ' B ', ' C ', ' d ', ' e ');     $size = count ($array);    //Gets the number of array cells     for ($i =0 $i < $size $i + +)         echo $array [$i]. ' <br/> ';      2, using foreach         foreach is more convenient and flexible, generally use it, when using foreach ($arr _name as $value), Use the AS keyword for the previous array for its elements, which, of course, is for a one-dimensional array, and you can also take the key name of the element in the following way, foreach ($arr _name as $key => $value).   <?php     $array = array (' OS ' => ' Linux ', ' Server ' => ' Apache ', ' db ' => ' MySQL ', ' language ' => ' PHP ');     foreach ($array as $key => $value) {        echo ' key: '. $key. '---Value: '. $value. ' <br/> ';    }    :     &NBsp            3, list, each, while function combined with         Each function in an array once, A pointer to an internal element moves backwards a cell, each time returning a fixed-form key/value array, specifically (1=> value, ' value ' => value, 0=> key, ' key ' => key). The next time each action is moved to the next element, example   <?php         $arr = Array (' One ' => ' a ', ' two ' => ' B ', ' three ' = > ' C ');     $LST = each ($arr);         echo ' each=> <pre> ';     Var_dump ($lst);                         LIS The function of the T function is to assign it an array variable, it assigns the key value of the element in the array to its own parameters from small to large, if the parameter is not enough, if the value in the array is not enough, then the parameter is assigned a null value, the code is connected to the       list ($key, $val) = $lst;     echo ' <br/> ';                     $LST The values in the array variables, the key values are integers in front of the 1=> ' a ' and the back 0=> ' The advantage of the One,list function is that even if the key value is small, the elements that are behind it are assigned to the former to the back of the list function in the order they were raised.         because each does not loop the array, each action only moves the pointer, to the end of the array return value of false, so put in the while is the most suitable for the       &NBsp   <?php     $arr = Array (' One ' => ' a ', ' two ' => ' B ', ' Three ' => ' C ');     while ($key, $val) = each ($arr) {        echo $key. ' => '. $val. ' <br/> '; &n Bsp                          4, using array internal pointer move function     & nbsp   Array internal pointers default to the first element in the array. function is roughly, current (): Returns the value of the element whose 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 position of the previous element; reset (): Moves the array pointer one to the position of the first element in the array; end (): Move the array pointer to the position of the last element in the array. They are the parameters of the array variable itself, and the combination of do...while can achieve the sequence of arrays and reverse traversal.           replication code <?php     echo ' key: '. Key ($arr). "Current:". Current ($arr). <br/> ';  //the current key and value by default to the first element of the array     next ($arr);  //, point to the second element     echo ' key: '. Key ($arr). ' Current: '. Current ($arr). ' <br/> ';  //current key and value     next ($arr);  /move back one, point to the third element     echo ' key: '. Key ($arr). Current ($arr). <br/> ';  //current key and value     prev ($arr);  //forward one, pointing to the second element     echo ' key: '. Key ($arr). ' Current: '. Current ($arr). ' <br/> ';  //the current key and value     End ($arr);    //moved to the last element of the array     echo ' key: '. Key ($arr). Current ($arr). <br/> ';  //current key and value     reset ($arr);  //moves to the first element of the array     echo ' key: '. Key ($arr). ' Current: '. Current ($arr). ' <br/> ';  //current key and value                  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.