PHP array Traversal

Source: Internet
Author: User

PHP array Traversal
Arrays are a very powerful weapon in PHP, which is convenient and easy to use. Due to exceptions and flexibility in use, it can be used to implement linked list, stack, queue, heap, so-called dictionary, set, and so on in the data structure. It can also be converted to XML format. 1. It is not a good choice to use the for statement to traverse the array. It is too limited because the subscript of the array is often discontinuous, or there are both integer subscripts and string subscripts, however, in such a case, it happens to be an index array, and its subscript is continuous, so this is also a method. <? Php $ array = array ('A', 'B', 'C', 'D', 'E'); $ size = count ($ array ); // obtain the number of array units for ($ I = 0; $ I <$ size; $ I ++) echo $ array [$ I]. '<br/>'; 2. It is more convenient and flexible to use foreach than for. Generally, foreach ($ arr_name as $ value) is used ), use the as keyword in the previous array for its elements. Of course, this is for a one-dimensional array. You can also obtain the key name of the element, use the following method: 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/>';}: 3. each time the list, each, and while functions work with the each function, each time they act on the array, the pointer to the internal element moves one unit backward, each returns a Fixed Array of key/value pairs, specifically (1 => value, 'value' => value, 0 => key, 'key' => key ). The next each will be moved to the next element. Example <? Php $ arr = array ('one' => 'A', 'two' => 'B', 'three '=> 'C '); $ lst = each ($ arr); echo 'Each => <pre> '; var_dump ($ lst); the function of list is to assign it an array variable, it assigns the key values of the elements in the array in the order of ascending values to their own parameters. If the parameter is not enough to fill the parameter, if the value in the array is not enough, the parameter is assigned a null value, and the code is connected to list ($ key, $ val) = $ lst; echo '<br/>'; $ value in the lst array variable, the key values are the first 1 => 'A' and the following 0 => 'one, the benefit of the list function is that, even if the key value is small, the elements behind the list will be assigned to the parameters in the list function in the ascending order. Because each does not loop the array, each only moves the pointer and returns false at the end of the array. Therefore, it is best to put it in while. <? Php $ arr = array ('one' => 'A', 'two' => 'B', 'three '=> 'C '); while (list ($ key, $ val) = each ($ arr) {echo $ key. '=> '. $ val. '<br/>';} 4. Use the internal pointer of the array to move the internal pointer of the Function Array to point to the first element in the array by default. The function generally has: current (): returns the element value of the current pointer pointing to the position in the array; key (): returns the element key of the current pointer pointing to the position in the array; next (): moves the pointer to the next element position; prev (): Move the pointer to the position of the previous element; reset (): Move the array pointer to the position of the first element of the array; end (): move the array pointer to the position of the last element of the array. They act on the parameters of the array variable itself, and can be combined with do... while to achieve array order and reverse traversal. Copy the Code <? Php echo 'key :'. key ($ arr ). 'Current :'. current ($ arr ). '<br/>'; // The current key and value. By default, it points to next ($ arr), the first element of the array. // move one back to echo 'key :'. key ($ arr ). 'Current :'. current ($ arr ). '<br/>'; // The current key and value next ($ arr); // move another key to the third element echo 'key :'. key ($ arr ). 'Current :'. current ($ arr ). '<br/>'; // The current key and value prev ($ arr); // forward one 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 the array echo 'key :'. key ($ arr ). 'Current :'. current ($ arr ). '<br/>'; // current key and Value

Related Article

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.