php教程

SplDoublyLinkedList::push

(PHP 5 >= 5.3.0, PHP 7)

SplDoublyLinkedList::pushPushes an element at the end of the doubly linked list

说明

public SplDoublyLinkedList::push ( mixed $value ) : void

Pushes value at the end of the doubly linked list.

参数

value

The value to push.

返回值

没有返回值。

User Contributed Notes

chicotec dot asm at gmail dot com 14-Feb-2019 09:00
You can also push an element at the end of the doubly linked list like in a simple array with square brackets:

$spl = new SplDoublyLinkedList;

$spl[] = 1;
$spl[] = 2;
$spl[] = 3;
$spl[] = 4;

print_r($spl);
lincoln dot du dot j at gmail dot com 11-Jul-2017 05:35
$spl = new SplDoublyLinkedList;

$spl->push(1);
$spl->push(2);
$spl->push(3);
$spl->push(8);
$spl->push(5);

print_r($spl);
Gilles A 30-Sep-2014 02:18
Be careful SplStack mode is LIFO (Last In First Out) not FIFO (First in First Out)
<?php
// Array (FIFO)
$stack = array();
array_push($stack,"orange");
array_push($stack,"banana");
array_push($stack,"apple");
array_push($stack,"raspberry");

var_dump($stack);
?>
// result

array (size=4)
  0 => string 'orange' (length=6)
  1 => string 'banana' (length=6)
  2 => string 'apple' (length=5)
  3 => string 'raspberry' (length=9)

<?php
// SplStack (LIFO)
$stack = new SplStack();
$stack ->push('orange');
$stack ->push('banana');
$stack ->push('apple');
$stack->push('raspberry');

$stack->rewind();
while(
$stack->valid()){
   
var_dump($stack->current());
   
$stack->next();
   
}
?>
//result
string 'raspberry' (length=9)
string 'apple' (length=5)
string 'banana' (length=6)
string 'orange' (length=6)

CopyRight © 2008-2022 verySource.Com All Rights reserved. 京ICP备17048824号-1 京公网安备:11010502034788