A comparative analysis of PHP one-dimensional array traversal method

Source: Internet
Author: User
    1. A
    2. $arr =array (' a ' = ' = ' abc ', ' B ' =>123, ' C ' =>true);
    3. B
    4. $arr =range (' A ', ' d ');
    5. 1
    6. for ($i =0; $i<>
    7. echo $arr [$i]. ', ';
    8. Echo '
      ';
    9. 2
    10. foreach ($arr as $key)
    11. echo "$key,";
    12. Echo '
      ';
    13. 3
    14. foreach ($arr as $key = $val)
    15. echo "$key-$val,";
    16. Echo '
      ';
    17. 4
    18. Reset ($arr);
    19. while ($item =each ($arr)) {
    20. echo $item [' key ']. ' -'. $item [' value ']. ', ';
    21. }
    22. Echo '
      ';
    23. 5
    24. Reset ($arr);
    25. while (list ($key, $val) =each ($arr)) {
    26. echo "$key-$val,";
    27. }
    28. Echo '
      ';
    29. ?>
Copy Code

Code Description:

Statement a $arr =array (' a ' = ' = ' abc ', ' B ' =>123, ' C ' =>true); Initialize the $arr to get a numeric index array, output:,,, ABC, 123, 1,A-ABC, b-123, C-1,A-ABC, b-123, C-1,A-ABC, b-123, c-1, using statement b $arr =range (' A ', ' d '); Initialize the $arr to get an associative array, output: A, B, C, d,a, B, C, D,0-a, 1-b, 2-c, 3-d,0-a, 1-b, 2-c, 3-d,0-a, 1-b, 2-c, D-Z, for loop only limited to numeric index; for and fore The reset () operation of the data does not need to be performed at the end of the ACH traversal for the next traversal, which is required by each method.

The above is the full content of today's PHP tutorial, hope to help you master the method of the PHP array traversal, the programmer's home, I wish you all learning progress.

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