PHP looping array and list combination-php Tutorial

Source: Internet
Author: User
PHP loop combined with array and list using foreach: 1. usage 1 & lt ;? Php ?? $ Arrarray (Zhang San, Li Si, Wang Wu, Ma Liu );?? Foreach ($ arras $ value )?? Echo $ value. & lt; br & gt ;;? & Gt; 2. usage 2 & lt ;? Php $ arrarray (chinese & gt; 130 PHP loop combining array and list usage

Use of foreach:
1. usage 1

?? $ Arr = array ("zhang san", "Li Si", "Wang Wu", "Ma Liu ");
?? Foreach ($ arr as $ value)
?? Echo $ value .'
';
?>


2. usage 2

$ Arr = array ('China' => 130, 'math' => 140, 'INC' => 135, 'computer '=> 130 );
Foreach ($ arr as $ key => $ value)
Echo $ key. ':'. $ value .'
';
?>


3. copy or reference
Copy by default, similar to the function:

???? $ Arr = array (1, 2, 4 );
???? Foreach ($ arr as $ value)
???? $ Value * = 2; // The actual change is that the copy has no effect on the original array.
???? Print_r ($ arr); // Array ([0] => 1 [1] => 2 [2] => 3 [3] => 4)
?>


However, you can change it to the reference method by using the & symbol:

???? $ Arr = array (1, 2, 4 );
???? Foreach ($ arr as & $ value)
???? $ Value * = 2;
???? Print_r ($ arr); // Array ([0] => 2 [1] => 4 [2] => 6 [3] => 8)
?>

4. for loop

$ Arr = array (1, 2, 3, 4, 5 );
For ($ I = 0; $ I Echo $ arr [$ I].'
';
?>


5. each/list

Each usage. each time a four-element array is taken out, where 0 corresponds to the key and 1 corresponds to the value:

$ Arr = array ("zhang san", "Li Si", "Wang Wu ");
$ Bar = each ($ arr );
Print_r ($ bar); // Array ([1] => Zhang San [value] => Zhang San [0] => 0 [key] => 0)
?>


List usage:

$ Arr = array ("zhang san", "Li Si", "Wang Wu ");
List ($ a [0], $ a [1], $ a [2]) = $ arr;
Print_r ($ a); // Array ([2] => Wang Wu [1] => Li Si [0] => Zhang San)
?>


Use list in combination with each:

$ Arr = array ("zhang san" => 20, "Li Si" => 29, "Wang Wu" => 23 );
While (list ($ key, $ value) = each ($ arr ))
Echo "$ key => $ value ".'
';
?>


Note that the while (list ($ key, $ value) = each ($ arr) each has two numeric indexes assigned to the two variables in the list.
So

$ Arr = array ("zhang san", 'B' => "Li Si", "Wang Wu ");
List ($ a [0], $ a [1]) = $ arr;
Print_r ($ a); // Array ([1] => Wang Wu [0] => Zhang San)
?>


6. multi-dimensional array

$ A = array ();
$ A ['Shandong '] [0] = "Jinan ";
$ A ['Shandong '] [1] = "Weihai ";
$ A ['heilongjiang '] [0] = "Harbin ";
$ A ['heilongjiang '] [1] = "Qiqihar ";

Foreach ($ a as $ key1 => $ v1 ){
?? Echo"$ Key1".'
';
???????? Foreach ($ v1 as $ v2 ){
???????????????? Echo "$ v2 \ n ";
????????}
???????? Echo'
';
}
?>


7. array functions

Key Value

$ Arr = array (
???????? 'Zhang San' => 'jinan ',
???????? 'Li si' => 'yantai ',
???????? 'Wang 5' => 'weihai ',
???????? 'Zhao Liu' => 'Beijing ',
????????);

While ($ person = current ($ arr )){
???????? If ($ person = 'weihai '){
???????????????? Echo key ($ arr ).'
';
????????}
???????? Next ($ arr );
}
?>

The above will get 'Wang Wu', but if Yantai is an "" empty string, the program will jump out of the loop here, and the empty string is converted to false.

Use for to implement foreach:

$ Arr = array (
???????? 'Baidu '=> 'http: // www.baidu.com ',
???????? 'Google '=> 'http: // www.google.com.hk ',
???????? '1970 phone' => 'http: // www.my400800.cn ',
???????? 'Bing' => 'http: // cn.bing.com ',
????????);

For (reset ($ arr); $ key = key ($ arr); next ($ arr ))
Echo $ key .'
';
?>


This problem is the same as above. if a key value is calculated as false, the loop will jump out.

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.