简体   繁体   English

如何清除或删除SplStack中的所有元素?

[英]How do I clear or delete all elements in an SplStack?

The SplStack has many functions for accessing its elements, but is there a way to clear it? SplStack有许多用于访问其元素的函数,但有没有办法清除它? Or should I "clear" my stack by replacing it with a new, empty stack? 或者我应该通过用新的空堆栈替换它来“清除”我的堆栈?

There is no built-in method to clear a stack. 没有内置方法来清除堆栈。

You'd either need to clear it down using pops or unsets: 你需要使用pops或unsets清除它:

while($stack->valid()) {
    $stack->pop();
}

Or unset the stack itself, and create a new one: 或者取消设置堆栈本身,并创建一个新堆栈:

unset($stack);
$stack = new \SplStack();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM