PHP Array Pointers Learning notes (i)

Source: Internet
Author: User
There is a problem with an array queue, the array length is fixed, and when an element is inserted into the input, the array auto-header begins to overwrite the fill after the maximum length is exceeded.

At this point, the entire array is formed into a ring. Such as.


Define an array

$test _array = Array ();
function Push_array ($value, & $test _array) {
if (count ($test _array) < 12) {

Add elements directly when the array is less than the specified length

$test _array[] = $value;
} else {

When the array is greater than the specified length, use each to remove the element of the current pointer and move the pointer down

List ($k, $v) = each ($test _array);

Generally use the modulus algorithm, get the key to be overwritten, and then directly overwrite it.

$k = $k% 12;
$test _array[$k] = $value;
}
}
Push_array (0, $test _array);
Push_array (1, $test _array);
Push_array (2, $test _array);
Push_array (3, $test _array);
Push_array (4, $test _array);
Push_array (5, $test _array);
Push_array (6, $test _array);
Push_array (7, $test _array);
Push_array (8, $test _array);
Push_array (9, $test _array);
Push_array (Ten, $test _array);
Push_array (one, $test _array);
Push_array ($test _array);
Push_array ($test _array);
Push_array ($test _array);
Push_array ($test _array);
Push_array ($test _array);
Push_array (n, $test _array);
Push_array ($test _array);
Push_array ($test _array);
Push_array ($test _array);
Push_array (+, $test _array);
Push_array ($test _array);
Push_array (at $test _array);
Push_array ($test _array);
Var_dump ($test _array); exit;

Test results:

Array (+) {  [0]=>  int ([1]=> int) [2]=> int (+)  [3]=>  Int (15)  [4]=>  Int (+)  [5]=>  Int (+)  [6]=>  Int ([7]=>)  Int (19)  [8]=>  int ([9]=> int  )  [10]=> Int (23)  [11]=>  int }

In addition to each, the following array pointer functions are also useful

    • Current ()-Returns the value of the present element in the array
    • End ()-Points the inner pointer to the last element in the array and outputs
    • Next ()-Points the inner pointer to the next element in the array and outputs
    • Prev ()-Points the internal pointer to the previous element in the array and outputs
    • Reset ()-Points the inner pointer to the first element in the array and outputs

The above describes the PHP array pointer Learning notes (a), including aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

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