PHP array cursor provides detailed explanation of various operations on the array, and php array cursor details _ PHP Tutorial

Source: Internet
Author: User
PHP array cursor provides detailed explanation of various operations on the array, and php array cursor details. PHP array cursor implements various operations on the array. php array cursor details This article analyzes the PHP array cursor to implement various operations on the array. I will share with you for your reference. for details about various operations on arrays, see PHP array cursors.

This article analyzes various operations on arrays through PHP array cursors. We will share this with you for your reference. The details are as follows:

How does one implement loops on arrays without using foreach?

A: We only need to simulate foreach. when the array executes the foreach loop, there is a cursor pointing to the value of the current array loop,

If we can get the cursor and operate the cursor to forward or backward the cursor, then we can implement a circular array without foreach.

Code directly, clear and intuitive:

<? Php $ arr = array ('you', 'Ruby', 'an', 'haok', 'you', 'yes', 'Qing', 'day '); // current () function is used to obtain the value of the cursor's array position. // next () is used to move the cursor to the end of the array. for (; $ v = current ($ arr ); next ($ arr) {echo $ v ,'
';}/* Return. if you are fine, it will be sunny. * // then how can we take the cursor two steps and two steps? For (; $ v = current ($ arr);) {echo $ v ,'
'; Next ($ arr) ;}// execution result, no response. why? // The cause is that the previous cyclic cursor has reached the tail of the array. // therefore, when executing current ($ arr), false is always returned, // therefore, the second loop will not be executed // Here we will use a reset () method to re-point the cursor to the array's header reset ($ arr); echo' '; For (; $ v = current ($ arr);) {echo $ v ,'
'; Next ($ arr) ;}// then how can we take two steps and take a step back? Reset ($ arr); echo' '; For ($ flag = true; $ v = current ($ arr);) {echo $ v ,'
'; If ($ flag) {next ($ arr); $ flag = false;} else {prev ($ arr ); $ flag = true;}/* returns the result. if you are well-protected, the weather is fine. * // How can you reverse the cycle? // The end () function is used here to point the cursor of the array to the end ($ arr); echo' '; For (; $ v = current ($ arr); prev ($ arr) {echo $ v ,'
';}/* Returned: the sky is fine. if you * // note that when the array value is equal to 0, false, null, undefined, the second part of the for loop returns false, which causes the loop to terminate reset ($ arr); echo' '; $ Arr2 = array ('you', 'Ruby', 0, 'haok', 'you', 'yes', 'Qing', 'day '); for (; $ v = current ($ arr2); next ($ arr2) {echo $ v ,'
';}/* Returns if */?>

In addition, you can use the key () function to obtain the keys pointed to by the cursor.

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.