Array Traversal of PHP

Source: Internet
Author: User

Arrays in PHP Chinese 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. Use for

The For statement traversal array is not a number selection, generally does 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.

<? PHP     $array Array (' A ', ' B ', ' C ', ' d ', ' e ');     $size Count ($array);    // get the number    of array cells  for ($i$i<$size$i+)         Echo $array [$i]. ' <br/> ';

2. Using foreach

foreach is more convenient and flexible than for, it is generally used, using foreach ($arr _name as $value), using the AS keyword for the previous array for its elements, of course, for a one-dimensional array, you can also take the key name of the element, A foreach ($arr _name as $key = = $value) is available as follows.

<? PHP     $array Array (' os ' = ' Linux ', ' server ' = ' Apache ', ' db ' = ' mysql ', ' language ' = ' PHP ');     foreach ($arrayas$key= +$value)        {echo ' key: '.  $key. '---value: '. $value. ' <br/> ';    }

Operation Result:

3, list, each, while function combination

Each function is applied to an array once, the pointer to the inner element is moved backwards through a cell, each time each returns a fixed-format key/value array, which is (1=> value, ' value ' = = value, 0=> key, ' key ' = ' key '). The next time each action is moved to the next element, the example

<? PHP         $arr Array (' One ' = ' a ', ' both ' = ' B ', ' three ' = ' C ');     $lst  each ($arr);         Echo ' Each=> <pre> ';     Var_dump ($lst

Effect

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, and if the argument is not enough to fill the argument, the argument is assigned a null value if the value in the array is not sufficient, and the code is then

    List ($key$val$lst;     Echo ' <br/> ';     Echo ' Key: '. $key. ' Value: '. $val. ' <br/> ';

Effect

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

<? PHP     $arr Array (' One ' = ' a ', ' both ' = ' B ', ' three ' = ' C ');      while (list($key$valeach ($arr)) {        echo $key. ' = '. $val. ' <br/> ';    }

Effect

4. Use the array inner pointer to move the function

The array inner pointer defaults to the first element in the array, the function is roughly, current (): Returns the value of the element that is pointing to the position in the array; key (): Returns the element key that 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 (): 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. The parameters they function are the array variables themselves.

<?PHPEcho' Key: '.Key($arr). ' Current: '. Current($arr).‘ <br/> ';//the current key and value, which points to the first element of the array by default    Next($arr);//move back one, 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: '. Current($arr).‘ <br/> ';//current key and value    prev($arr);//move forward one, point to the second element    Echo' Key: '.Key($arr). ' Current: '. Current($arr).‘ <br/> ';//current key and value    End($arr);//move to the last element of the array    Echo' Key: '.Key($arr). ' Current: '. Current($arr).‘ <br/> ';//current key and value    Reset($arr);//move to the first element of an array    Echo' Key: '.Key($arr). ' Current: '. Current($arr).‘ <br/> ';//current key and value

Effect

Array Traversal of PHP

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.