After knowing the concepts of arrays, it’s time to learn other data-structures of PHP offered. SPL provides a set of standard data structures. They are grouped here by their underlying implementation which usually defines their general field of application.

  • Doubly Linked Lists

A Doubly Linked List (DLL) is a list of nodes linked in both directions to each other. Iterator’s operations, access to both ends, addition and removal of nodes have a cost of O (1) when the underlying structure is a DLL. It hence provides a decent implementation for stacks and queues.

Generally we can implement doubly linked list in PHP through SplDoublyLinkedList class


The SplDoublyLinkedList class provides the main functionalities of a doubly linked list.


  • Iteration Direction


The list will be iterated in a last in, first out order, like a stack.


The list will be iterated in a first in, first out order, like a queue.

  • Iteration Behavior


Iteration will remove the iterated elements.


Iteration will not remove the iterated elements.

Let’s do some operations on linked list….

Methods to remember:

  • SplDoublyLinkedList() -> use to create linked list
  • push() -> helps to push elements at the end of the list.
  • top() -> prints the top of the linked list.
  • bottom() -> print the first element of the LL.

File – code1.php

//FIFO and LIFO in SplDoublyLinkedList
$list = new SplDoublyLinkedList();
echo “FIFO (First In First Out) :\n”;
for ($list->rewind(); $list->valid(); $list->next()) {
    echo $list->current().”\n”;
echo “<br>LIFO (Last In First Out) :\n”;
for ($list->rewind(); $list->valid(); $list->next()) {
    echo $list->current().”\n”;
echo “<br> linked list with keys: \n “;
echo “<br>top of the element in Linked list “;
echo $list->top();
echo PHP_EOL;
echo “<br> bottom(first) element on of linked list: “;
//Array first value
echo $list->bottom();
echo PHP_EOL;
echo “<br>linked list length: “;
echo $list->count();

  • Stack – this data-structure work as LIFO(Last-In-first-Out) order.

The SplStack class


The SplStack class provides the main functionalities of a stack implemented using a doubly linked list.

Methods and usage:

add() -> SplDoublyLinkedList::add — Add/insert a new value at the specified index


public SplDoublyLinkedList::add(int $index, mixed $value): void

Insert the value value at the specified index, shuffling the previous value at that index (and all subsequent values) up through the list.

  • push() -> push element into top of the stack(insert from end into stack)
  • pop() -> pop element from stack.(top of the stack)
  • top() -> print the top of the stack.

File – code2.php

//SplStack Mode is LIFO (Last In First Out)
$stk = new SplStack();
// you can add element individually…
$stk[] = 1;
$stk[] = 20;
$stk[] = -3;
// you can also use push() method to insert element into stack from top of stack
echo ” stack is: “;
foreach ($stk as $elem)  {
    echo $elem.”\n”;
// top of the stack is using top() method
echo “<br> top of the stack is: “;
echo $stk->top();
// pop operation using pop() method
$n = $stk->pop();
echo “<br> pop element is: $n”;
// 3 times doing pop() operation..
echo “<br> Now, stack is: “;
foreach ($stk as $elem)  {
    echo $elem.”\n”;
   // adding element at specific location using add() method.
// adding value ‘420’ at index->1, value ‘560’at index->3
echo “<br> Now, stack is: “;
foreach ($stk as $elem)  {
    echo $elem.”\n”;

  • Queue

Queue data-structure inserts elements at FIFO order.


The SplQueue class provides the main functionalities of a queue implemented using a doubly linked list.

Class synopsis

class SplQueue extends SplDoublyLinkedList {

/* Inherited constants */

public const int SplDoublyLinkedList::IT_MODE_LIFO;

public const int SplDoublyLinkedList::IT_MODE_FIFO;

public const int SplDoublyLinkedList::IT_MODE_DELETE;

public const int SplDoublyLinkedList::IT_MODE_KEEP;

Methods usage…

  • SplQueue() -> helps to create queue
  • enqueue() -> SplQueue::enqueue — Adds an element to the queue(insert from first-end)
  • dequeue() -> delete element from last-end.
  • count() -> helps to count length of queue.

File – code3.php

$q = new SplQueue();
// insert item into Queue
// insert item manually…
echo “Queue is: “;
foreach ($q as $item) {
    echo ” “.$item;
// dequeue() method helps to ‘dequeue’ operation…
// remove element->20
// remove element->40
echo “<br> after dequeue, Queue becomes: “;
foreach ($q as $item) {
    echo ” “.$item;
echo “<br> Queue length is: “.$q->count();