PHP array (previous) traversal

Source: Internet
Author: User
PHP array (previous) traversal
 Demo = array (0 => 'apple', 1 => 'Orange ', 2 => 'bana'); * 2, associated array: key-value form * 3, multi-dimensional array: array $ demo = array (), array (); * // ** array traversal * 1, for * this method is not preferred * array must be a subscript continuous index array ** 2, foreach * foreach ($ arr as $ item ){...} * foreach ($ arr as $ key => $ val ){...} ** 3, while () list () each () combination * each (): an array is required as the parameter, and array (, $ key, $ val) is returned ), is a cursor operation. after execution, false * list (): a, list () = array () is returned. the array must be assigned to list () * B, the number of elements in the array must be the same as the number of parameters in the list () function * c. each element in the array is assigned to each parameter * d, list () in list () only the index array ** e can be accepted, and the internal pointer control function ** prev ($ arr) is traversed in the subscript order of the index: the previous pointer * next ($ arr ): next pointer * reset ($ arr): First * end ($ arr): Last ** current ($ arr): current key * key ($ arr ): current value ** // forecho "\ n... \ n "; $ demo = array ('apple', 'Orange ', 'banana'); for ($ I = 0; $ I
 
  
'Apple', 1 => 'Orange ', 2 => 'banana'); for ($ I = 0; $ I
  
   
$ Val) {echo ''. $ key. '=> '. $ val. '';} echo" \ n ". '------------------------------------'; $ user = array ('id' => 1, 'name' => 'Jack', 'age' => 10, 'Sex '=> 'male'); // whileecho "\ n while... \ n "; while ($ arr = each ($ user) {//" $ arr [0] --> $ arr [1] "; echo $ arr ['key']. "--> ". $ arr ['value']. "";} echo "\ n -----------------------------------------"; while (list ($ key, $ value) = each ($ user) {echo ''. $ key. '-----> '. $ value. "";} echo "\ n ------------------------------------------- \ n"; reset ($ user); while (list ($ key, $ value) = each ($ user )) {echo ''. $ key. '-----> '. $ value. "";} echo "\ n ------------------------------------------- \ n"; while (list ($ key, $ value) = each ($ user) {echo ''. $ key. '-----> '. $ value. "";} reset ($ user); next ($ user); echo "\ n ------------------------------------------- \ n"; while (list ($ key, $ value) = each ($ user) {echo ''. $ key. '-----> '. $ value. "";} reset ($ user); echo "\ n"; echo current ($ user ). '----------> '. key ($ user ). "\ n"; end ($ user); echo current ($ user ). '----------> '. key ($ user ). "\ n"; prev ($ user); echo current ($ user ). '----------> '. key ($ user ). "\ n";/** result *... apple orange banana... apple orange banana foreach... apple orange banana foreach... 0 => apple 1 => orange 2 => banana ------------------------------------------ while... id --> 1 name --> jack age --> 10 sex --> male sex id -----> 1 name -----> jack age -----> 10 sex -----> male sex age -----> 10 sex -----> male1 ----------> idmale ----------> sex10 ----------> age */
  
 

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.