PHP Conference Nagoya 2025

SplStack 类

(PHP 5 >= 5.3.0, PHP 7, PHP 8)

简介

SplStack 类的主要功能是通过将迭代模式设置为 SplDoublyLinkedList::IT_MODE_LIFO 来提供使用双向链表实现的栈。

示例

示例 #1 SplStack 示例

<?php
$q
= new SplStack();
$q[] = 1;
$q[] = 2;
$q[] = 3;
foreach (
$q as $elem) {
echo
$elem."\n";
}
?>

以上示例会输出:

3
2
1

添加备注

用户贡献的备注 2 notes

up
41
lsroudi at gmail dot com
10 years ago
the SplStack is simply a SplDoublyLinkedList with an iteration mode IT_MODE_LIFO and IT_MODE_KEEP
up
8
lincoln dot du dot j at gmail dot com
7 years ago
<?php
//SplStack Mode is LIFO (Last In First Out)

$q = new SplStack();

$q[] = 1;
$q[] = 2;
$q[] = 3;
$q->push(4);
$q->add(4,5);

$q->rewind();
while(
$q->valid()){
echo
$q->current(),"\n";
$q->next();
}
?>

Output
5
4
3
2
1
To Top