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!
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: