PHP Development (21)-use foreach, list, while, and each to traverse arrays-PhpStorm

Source: Internet
Author: User
There are many ways to traverse arrays. in other languages, arrays with consecutive subscripts use for traversal. However, based on the particularity of PHP arrays, we generally use foreach and sometimes each. Foreach:

There are many ways to traverse arrays. in other languages, arrays with consecutive subscripts use for traversal. However, based on the particularity of php arrays, we generally use foreach and sometimes each.

First, let's take a look at the arrays in most languages: $ arr = array ("a", "B", "c", "d", "e", "f ", "g ");

Then let's take a look at the array format that can exist in PHP: $ arr2 = array ("a", "B", 100 => "c", "d ", "xxx" => "e", "f", "g ");

Yes, arr2 cannot use for traversal ~~ Therefore, we have a powerful foreach method ~~

Foreach has two expressions:

1. foreach (array as custom variable)

2. foreach (array as subscript variable => value variable)


List:

List () assigns values to a group of variables in one operation. Note: list can only convert arrays with consecutive indexes into variables.

I personally think list has no advantages except when used with the explode function. Explode () splits the string into an array.

The following code demonstrates an example: list ($ name, $ web) = explode ("_", $ str );


Each:

Each () returns the key name and key value of the current element, and moves the internal pointer forward.

Current ()-returns the value of the current element in the array.

End ()-points the internal pointer to the last element in the array and outputs it.

Next ()-point the internal pointer to the next element in the array and output it.

Prev ()-points the internal pointer to the previous element in the array and outputs it.

Reset ()-points the internal pointer to the first element in the array and outputs it.

With while, you can traverse the array: while ($ tmp = each ($ arr) {print_r ($ tmp); echo'
';}

 Value variable) */$ arr = array ("a", "B", "c", "d", "e", "f", "g "); $ arr2 = array ("a", "B", 100 => "c", "d", "xxx" => "e", "f ", "g"); $ group = array ("name" => "iwanghang", "age" => 18, "sex" => "male ", "email" => "iwanghang@qq.com"), // $ group [0] array ("name" => "queen", "age" => 14, "sex" => "female", "email" => "queen@qq.com"), // $ group [1] array ("name" => "king ", "age" => 55, "sex" => "male", "email" => "king@qq.com"), // $ group [2]); echo '---------- use for to traverse the array ----------
'; For ($ I = 0; $ I ';}/* Print the result: a B c d e f g */echo' ---------- use foreach to traverse the array ----------
'; Foreach ($ arr2 as $ value) {echo $ value .'
';}/* Print the result: a B c d e f g */echo' ---------- use foreach to traverse array 2 ----------
'; Foreach ($ arr2 as $ bb => $ vv) {echo $ bb.' ----- '. $ vv .'
';}/* Print the result: 0 ----- a 1 ----- B 100 ----- c 101 ----- d xxx ----- e 102 ----- f 103 ----- g */echo' ---------- Print the two arrays ----------
'; Echo'
';    print_r($group);    echo '
';/* Print the result: array ([0] => Array ([name] => iwanghang [age] => 18 [sex] => male [email] => iwanghang@qq.com) [1] => Array ([name] => queen [age] => 14 [sex] => Female [email] => queen@qq.com) [2] => Array ([name] => king [age] => 55 [sex] => male [email] => king@qq.com) */echo' '; Echo' '; Foreach ($ group as $ kk => $ row) {echo' '; If (is_array ($ row) {foreach ($ row as $ col) {echo' ';}} Else {echo' ';} Echo'
Convert an array to a table
'. $ Col .'';} Echo'
';/*** List () assigns values to a group of variables in one operation. * Note: The list can only convert the arrays with consecutive indexes to the variable ** explode () and scatter the strings into arrays. */Echo '---------- use list to traverse the array ----------
'; List ($ a, $ B, $ c) = array ("Guo Jing", "Huang Rong", "Hong Qigong"); echo $ ."
"; // Print the result: Guo Jing echo $ B ."
"; // Print the result: Huang Rong echo $ c ."
"; // Print the result: Hong Qigong echo '---------- use list to traverse array 2 ----------
'; List ($ aa, $ bb, $ cc) = array ("Guo Jing", "two" => "Huang Rong", "Hong Qigong"); echo $ aa ."
"; // Print the result: Guo Jing echo $ bb ."
"; // An error is reported because the index is not a continuous array echo $ cc ."
"; // Print the result: Hong Qigong echo '---------- use list to traverse array 3 ----------
'; $ Str = "iwanghang_CSDN"; list ($ name, $ web) = explode ("_", $ str); echo $ name ."
"; // Print the result: iwanghang echo $ web ."
"; // Print the result: CSDN/*** each () returns the key name and key value of the current element, and moves the internal pointer forward. ** Related method: * current ()-returns the value of the current element in the array * end ()-points the internal pointer to the last element in the array and outputs * next () -point the internal pointer to the next element in the array and output * prev ()-point the internal pointer to the previous element in the array and output * reset () -point the internal pointer to the first element in the array and output */echo '---------- use each to traverse the array ----------
'; $ Arr3 = ["" => "Guo Jing", "Huang Rong", "Hong Qigong"]; $ people = each ($ arr3); echo'
';    print_r($people);    echo '
';/** Print the result: Array ([1] => Guo Jing [value] => Guo Jing [0] => 0 [key] => 0) */$ people = each ($ arr3); print_r ($ people); // print the result: array ([1] => Huang Rong [value] => Huang Rong [0] => 1 [key] => 1) echo"
"; $ People = each ($ arr3); print_r ($ people); // print the result: array ([1] => Hong Qi Gong [value] => Hong Qi Gong [0] => 2 [key] => 2) echo"
"; $ People = each ($ arr3); print_r ($ people); // print the result: Empty echo"
";/***/Echo '---------- use while with each to traverse the array ----------
'; $ Arr4 = ["" => "Guo Jing", "Huang Rong", "Hong Qigong", "old urchin", "yellow pharmacist"]; while ($ tmp = each ($ arr4) {print_r ($ tmp); echo'
';}/** Printed result: Array ([1] => Guo Jing [value] => Guo Jing [0] => Lead [key] => Lead) array ([1] => Huang Rong [value] => Huang Rong [0] => 0 [key] => 0) array ([1] => Hong Qi Gong [value] => Hong Qi Gong [0] => 1 [key] => 1) array ([1] => old urchin [value] => old urchin [0] => 2 [key] => 2) array ([1] => yellow pharmacist [value] => yellow pharmacist [0] => 3 [key] => 3) */echo '---------- use while with each to traverse Array 2 ----------
'; $ Arr5 = ["" => "Guo Jing", "Huang Rong", "Hong Qigong", "old urchin", "yellow pharmacist"]; while ($ tmp = each ($ arr5) {// echo "{$ tmp ['key'] }=>{ $ tmp ['value']}"; echo "{$ tmp [0] }=>{$ tmp [1]}"; // echo "{$ tmp ['key'] }=> {$ tmp ['value']}"; same echo'
';}/** Print the result: lead => Guo Jing 0 => Huang Rong 1 => Hong Qigong 2 => old urchin 3 => Huang pharmacists * // *****/$ arr6 = ["one" => "Guo Jing", "two" => "Huang Rong", "three" => "Hong Qigong", "four" => "old urchin", "five" => "yellow pharmacist"]; echo "current location (first by default ):". key ($ arr6 ). "=> ". current ($ arr6 )."
"; // Current location (first by default): one => Guo Jing next ($ arr6); next ($ arr6); next ($ arr6 ); echo "Current location :". key ($ arr6 ). "=> ". current ($ arr6 )."
"; // Current location: four => old urchin end ($ arr6); echo" Current location :". key ($ arr6 ). "=> ". current ($ arr6 )."
"; // Current location: five => yellow pharmacist prev ($ arr6); prev ($ arr6); echo" Current location :". key ($ arr6 ). "=> ". current ($ arr6 )."
"; // Current location: three => Hong Qigong reset ($ arr6); echo" Current location :". key ($ arr6 ). "=> ". current ($ arr6 )."
"; // Current location: one => Guo Jing

Above is PHP development (21)-use foreach, list, while, and each to traverse the array-PhpStorm content. For more information, see PHP Chinese network (www.php1.cn )!

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.