There is such a problem. In an array queue, the array length is fixed. When inserting elements into the input, after the maximum length is exceeded, the array automatically starts overwriting and filling.
At this time, the entire array forms a ring. As shown below.When the array is less than the specified length, add elements directly
use using each to take out the element of the current pointer and move the pointer down
// Generally use the mold taking algorithm, obtain the key to be covered, and then directly overwrite
$ k = $ k % 12;
$test_array[$k] = $value;
}
push_array(0,$test_array);
push_array(2,$test_array);
push_array(3,$test_array);
push_array(5,$test_array);
push_array(6,$test_array);
push_array(8,$test_array);
push_array(9, $test_array);push_array(10,$test_array);
push_array(11,$test_array);
push_array(13,$test_array);
push_array(14,$test_array);
push_array(15,$test_array);
push_array(16,$test_array);
push_array(17,$test_array);
push_array(18,$test_array);
push_array(19,$test_array);
push_array(20,$ test_array);
push_array(21,$test_array);
push_array(22,$test_array);
push_array(23,$test_array);
push_array(24,$test_array);
var_dump($test_array);exit;
Test results:
array(12) { [0]=> int(24) [1]=> int(13) [2]=> int(14) [3]=> int(15) [4]=> int(16) [5]=> int(17) [6]=> int(18) [7]=> int(19) [8]=> int(20) [9]=> int(21) [10]=> int(22) [11]=> int(23) }