PHP's Array traversal _php tutorial

Source: Internet
Author: User

Array Traversal of PHP


Arrays in PHP is a very powerful weapon, easy to use, easy, because of the use of unusually flexible, it can be used to achieve the data structure of the linked list, stacks, queues, heaps and so-called dictionaries, collections, etc., can also be converted into XML format. 1, the use for a statement to traverse the array is not a good choice, generally do not use, the limitations are too large, because the array subscript many times discontinuous, or there are both integer subscript and string subscript, but there is such a situation, exactly the index array, and its subscript is continuous, then this is also a method. '; 2, using foreach foreach than for more convenient, flexible, generally use it, using a foreach ($arr _name as $value), the previous array using the AS keyword specifically for its elements, of course, for a one-dimensional array, You can also take the key name where the element is located, in the following way foreach ($arr _name as $key + = $value). ' Linux ', ' server ' = ' Apache ', ' db ' = ' mysql ', ' language ' = ' PHP '; foreach ($array as $key = + $value) {echo ' key: '. $key. '---Value: '. $value. '
'; }: 3. The list, each, and while function combines each function to an array at a time, the pointer to the inner element moves backward through a cell, each time each returns a fixed-format key/value array, (1=> value, ' value ' = value, 0 = > Key, ' key ' = = key). The next time each action is moved to the next element, the example ' A ', ' both ' = ' B ', ' three ' = ' C '; $lst = each ($arr); Echo ' each=>
';                    Var_dump ($LST); The function of the list function is to assign it an array variable, which assigns an integer key value in the array to its parameters from small to large, if the argument is not enough to fill the argument, if the value in the array is not enough, the parameter is assigned to a null value, and the code is connected to the list ($key, $val) = $    Lst Echo '
'; $lst the value of the array variable, the key value is the integer, respectively, the advantage of the preceding 1=> ' a ' and the 0=> ' one,list function behind it is that even if the key value is small, the following elements are assigned to the front-to-back parameters in the list function in order from small to large. Since each does not loop the array, each action will only move the pointer, to the end of the array to return a value of false, so put in while in the most appropriate. ' A ', ' both ' = ' B ', ' three ' = ' C '; while (list ($key, $val) = each ($arr)) {echo $key. ' + '. $val. '
'; } 4, using an array of internal pointers to move the inner pointer of the function array to the first element in the array by default, the function is roughly, current (): Returns the value of the element to the position in the array, key (): Returns the element key that the current pointer points to the position in the array; NEX T (): Moves the pointer to the next element position; Prev (): Moves the pointer to the position of the previous element, reset (): The array pointer one to the position of the first element of the array; end (): Moves the array pointer to the position of the last element of the array. They are the parameters of the array of variables themselves, and the combination of do...while can also be implemented in order and reverse sequence traversal. Copy Code '; The current key and value, default to the first element of the array next ($arr); Move back one, pointing to the second element, Echo ' key: '. Key ($arr). ' Current: '. Current ($arr). '
'; Current key and value next ($arr); Then move back one, pointing 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.html www.bkjia.com true http://www.bkjia.com/PHPjc/893562.html techarticle PHP array Traversal array in PHP is a very powerful weapon, easy to use, because of the use of unusually flexible, it can be used to achieve the data structure of the linked list, stack, team ...

  • 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.