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.