PHP foreach SplPriorityQueue - 找不到元素
我有以下代码:
$friendsOrdered= new SplPriorityQueue();
... // Code to fill up the priority queue
print_r($friendsOrdered);
$i = 0;
foreach($friendsOrdered as $f) {
}
echo "<br><br>";
print_r($friendsOrdered);
当我查看打印的输出时,我可以看到循环之后优先级队列为空。有没有办法阻止 foreach 删除元素?
谢谢!
I have the following code:
$friendsOrdered= new SplPriorityQueue();
... // Code to fill up the priority queue
print_r($friendsOrdered);
$i = 0;
foreach($friendsOrdered as $f) {
}
echo "<br><br>";
print_r($friendsOrdered);
When I look at the output of the prints I can see that after the loop the priority queue is empty. Is there a way of stopping the foreach from removing elements?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法阻止循环使元素出队,因为这是数据结构的本质。但是,您可以在循环之前创建对象的副本,这样您的数据就不会丢失:
You cannot stop the loop from dequeuing elements, as that is the nature of the data structure. You could, however, create a copy of the object before the loop so your data isn't lost: