Each () in the php--Array, list (), and while loop iterate through the array

Source: Internet
Author: User

. While traversal

In each loop of the while () statement, each () statement assigns the key of the current array element to the first argument variable $key of the list () function. and assigns the value in the current array element to the second parameter variable $value in the list () function, and each () statement then moves the pointer inside the array one step back, so that the next time () statement loops, it gets the key/value pair for the next element in the array. Until the end of the array each () statement returns the False,while () statement to stop the loop, ending the traversal of the array.

<body><?PHP//each () function takes an array of element information that the current pointer points to, and returns an array with key-value pairs, one at a time, once for each execution .$arr=Array(1=> "AA",2=> "BB",3=> "CC",4=> "dd",5=> "ee");//Associative ArraysPrint_r( each($arr)); Echo"<br/>";Print_r( each($arr)); Echo"<br/>";Print_r( each($arr)); Echo"<br/>";Print_r( each($arr)); Echo"<br/>";Print_r( each($arr)); Echo"<br/>";Print_r( each($arr));//list () special function//the only one "=" on the right side of the function, the right can only write an array//only for the index array//to map each element of the right array to the left variable$arra=Array(1,2,3,4,5,6);List($a,$b,$c,$d,$e,$f)=$arra;Echo $a; Echo"<br/>";//list ($a, $b, $d, $e, $f) = $arra, or//while loops, each (), list () iterates through the array while(List($key,$value)= each($arra)){    Echo"$key-$value<br/> "; }? ><?PHP//Each () and list () combination$contact=Array("ID" = 1, "name" = "Gao MoU", "Company" = "Company A", "address" = "Beijing");List($key,$value)= each($contact);Echo"$key=$value"; Echo"<br/>";List($key,$value)= each($contact);Echo"$key=$value";? ><br/><?PHP$contact=Array("ID" = 1, "name" = "Gao MoU", "Company" = "Company A", "address" = "Beijing"); while(List($key,$value)= each($contact)){    Echo"$key=$value. <br/> "; }?></body>

Each () in the php--Array, list (), and while loop iterate through the array

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.