PHP array cursor Implementation of various operations of arrays, PHP array cursor detailed _php tutorial

Source: Internet
Author: User

PHP array cursors implement a variety of operations on arrays, PHP array cursors are detailed


In this paper, an example of PHP array cursors is analyzed to implement various operations of arrays. Share to everyone for your reference, as follows:

How do I loop through arrays without a foreach implementation?

A: We just need to simulate foreach, and when the array executes the Foreach loop, there is a cursor pointing to the value that the current array is looping to.

So if we can get this cursor and manipulate the cursor to move the cursor forward or backward, then we can implement the loop array without foreach.

Directly on the code, clear and intuitive:

<?php $arr = Array (' You ', ' if ', ' ann ', ' good ', ' then ', ' yes ', ' clear ', ' Day '); the//current () function is the value of the array position where the cursor is located//next () is to move the cursor to the tail of the array for (; $v = Current ($arr), Next ($arr)) {echo $v, '
';} /* Return to you if it is sunny *///so how to make the cursor 2 Step 2 step? for (; $v =current ($arr);) {echo $v, '
'; Next ($arr); Next ($arr);} Execution results, no response, what is this for? The reason is that the previous loop cursor has reached the end of the array//Therefore, when the current ($arr) is executed always returns false,//so the second loop is not executed//So here we will use a reset () method to make the cursor re-pointing to the head of the array reset ($arr); Echo ' ' for (; $v =current ($arr);) {echo $v, '
'; Next ($arr); Next ($arr);} Then how to walk 2 steps, take a step back? Reset ($arr); Echo ' ' For ($flag =true; $v =current ($arr);) {echo $v, '
'; if ($flag) {next ($arr); Next ($arr); $flag =false; }else{prev ($arr); $flag =true; }}/* return you ann if good is fine is clear is fine *///how to reverse cycle? The end () function is used here, so that the cursor of the array points to the trailing end ($arr); Echo ' ' for (; $v =current ($arr);p rev ($arr)) {echo $v, '
';} /* Return: Sunny is good if you *///notice when the value of the array equals 0,false,null,undefined, "", the second part of the For loop returns false, which causes the loop to terminate reset ($arr); Echo ' '; $arr 2 = Array (' You ', ' if ', 0, ' good ', ' then ', ' yes ', ' clear ', ' Day '); for (; $v =current ($arr 2); next ($arr 2) {echo $v, '
';} /* return If you */?>

In addition to these, you can use the key () function to get the key that the cursor points to.

More readers interested in PHP array related content can view this site topic: PHP Array (arrary) Summary

I hope this article is helpful to you in PHP programming.

Articles you may be interested in:

    • PHP array de-weight relatively fast implementation mode
    • The traversal method of multidimensional array in thinkphp
    • Summary of examples of array processing functions in PHP
    • We should all master the use of PHP associative array techniques
    • Simple implementation method of PHP multidimensional array to one-dimensional array
    • Php method for sorting two-dimensional arrays by specified key values
    • How PHP deletes repeating elements in an array
    • PHP Multidimensional Array Traversal method (2 implementation methods)
    • A summary of some common functions of increment, delete and interpolation of logarithm groups in PHP
    • Explain the definition of PHP arrays and how to create them
    • A method for iterating through multidimensional arrays in PHP

http://www.bkjia.com/PHPjc/1096145.html www.bkjia.com true http://www.bkjia.com/PHPjc/1096145.html techarticle PHP array cursor Implementation of the various operations of arrays, PHP array cursor Detailed examples of this article analyzed the PHP array cursors to implement the various operations of the logarithm group. Share to everyone for your reference, specific ...

  • 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.