我有一个使用 PHP 制作的循环队列,其中包含您在文件中看到的所有通用函数,例如出队、入队、窥视等queue.php
我正在尝试使用已预先清理到文件save.php
中的AJAX 提交表单数据,该文件queue.php
包含在文件中。
队列.php
#prior queue code omitted as it is not relevant to the question
public function enqueue(string $element) {
$this->queue[$this->rear] = $element;
$this->rear = ($this->rear + 1) % $this->limit;
}
}
保存.php
include("queue.php");
$queue = new CircularQueue(5);
if (isset($_POST['data'])){
try{
$queue->enqueue($_POST['data']);
echo $queue->peek();
echo print_r($queue);
} catch (Exception $e){
echo $e->getMessage();
}
}
它成功地将一个 POST 数据排入队列,但任何后续的 AJAX 都会重置数组并继续将其存储为第一个索引。
例如,第一个 AJAX 数据:20
[0] ==> 20
第二个 AJAX 数据:285
[0] ==> 285
我检查了我的队列,它在单独的行中排队时按预期运行,所以问题出在save.php
文件中。
save.php
目标:我希望使用 AJAX发送到此文件的任何数据都相应地排队。
例如,第一个 AJAX 数据:20
[0] ==> 20
第二个 AJAX 数据:285
[0] ==> 20 [1] ==> 285