Use an array pointer to traverse the array, FOR/FOREACH to traverse the array, and foreach

Source: Internet
Author: User

Use an array pointer to traverse the array, FOR/FOREACH to traverse the array, and foreach

1. traverse one-dimensional arrays with array pointers

<? Phpheader ("Content-type: text/html; charset = UTF-8");/* use an array pointer to traverse the value of an array */$ arr = array ('A ', 'B', 'C', 'D'); echo current ($ arr); // returns the value of the current Unit of the pointer echo next ($ arr ); // The pointer moves to the next unit echo prev ($ arr); // the pointer moves to the previous unit and returns the value of the unit echo end ($ arr ); // move the pointer to the last unit echo reset ($ arr); // reset the Array (move the pointer to the beginning of the array) for ($ I = 0; $ I <count ($ arr ); $ I ++) {echo current ($ arr); // output the unit value of the current pointer a next ($ arr); // move the pointer down, output unit value}

2. FOR traversing two-dimensional arrays

$arr = array(        array('a','b','c','d'),        array('e','f','g','h'),        array('i','j','k','l','m','n'),    );for($i=0;$i<count($arr);$i++){    for($j=0; $j<count($arr[$i]); $j++)        echo $arr[$i][$j].' , ';      }

3. FOREACH traverses two Arrays

$ Arr = array ('A', 'B', 'C', 'D'), array ('E', 'F', 'G ', 'H'), array ('I', 'J', 'k', 'l', 'M', 'n', '0 '),); foreach ($ arr as $ key = >$ value) {foreach ($ arr [$ key] as $ subkey => $ subval) {// here, the first array parameter foreach uses $ arr [$ key] to represent the second-dimensional array echo $ subval. ',';}}

 

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.