PHP - Ds Deque::remove() Function



The PHP Ds\Deque::remove() function removes an element from the current deque at the specified index and returns the removed element. The "index" is the position of elements in the deque starting at 0, representing the first element, 1 representing the second element, and so on.

If the specified index parameter value is invalid, either negative or greater than the deque size, this function will throw an "OutOfRangeException" exception.

Syntax

Following is the syntax of the PHP Ds\Deque::remove() function −

public Ds\Deque::remove(int $index): mixed

Parameters

This function accepts a single parameter called 'index', which is described below −

  • index − An integer value which represents the index of the value to remove.

Return value

This function returns the value that was removed.

Example 1

Removing the first element of the deque.

The following is the basic example of the PHP Ds\Deque::remove() function −

<?php
   $deque = new \Ds\Deque([10, 20, 30, 40, 50]);
   echo "The original deque elements are: \n";
   print_r($deque);
   $index = 0;
   echo "Index value:".$index;
   echo "\nThe removed element is: ";
   print_r($deque->remove($index));
?>

Output

The above program produces the following output −

The original deque elements are:
Ds\Deque Object
(
    [0] => 10
    [1] => 20
    [2] => 30
    [3] => 40
    [4] => 50
)
Index value:0
The removed element is: 10

Example 2

Following is another example of the PHP Ds\Deque::remove() function. We use this function to remove an element from this deque (['a', 'b', 'c']) at the specified index 2

<?php
   $deque = new \Ds\Deque(['a', 'b', 'c']);
   echo "The original deque elements are: \n";
   print_r($deque);
   $index  = 2;
   echo "Index value: ".$index;
   echo "\nThe removed element is: ";
   print_r($deque->remove($index));
   echo "\nThe deque after removed: \n";
   print_r($deque);
?>

Output

After executing the above program, it will display the following output −

The original deque elements are:
Ds\Deque Object
(
    [0] => a
    [1] => b
    [2] => c
)
Index value: 2
The removed element is: c
The deque after removed:
Ds\Deque Object
(
    [0] => a
    [1] => b
)

Example 3

If the specified index value is invalid, this function will throw anOutOfRangeExceptionas follows:

<?php
   $deque = new \Ds\Deque(["Tutorials", "Point"]);
   echo "The original deque: \n";
   print_r($deque);
   $index = 5;
   echo "Index value: ".$index;
   echo "\nThe removed element: ";
   print_r($deque->remove($index));
?>

Output

Once the above program is executed, it will generate the following output −

The original deque:
Ds\Deque Object
(
    [0] => Tutorials
    [1] => Point
)
Index value: 5
The removed element: PHP Fatal error:  Uncaught OutOfRangeException: 
Index out of range: 5, expected 0 <= x >= 1 in C:\Apache24\htdocs\index.php:8
Stack trace:
#0 C:\Apache24\htdocs\index.php(8): Ds\Deque->remove(5)
#1 {main}
  thrown in C:\Apache24\htdocs\index.php on line 8
Advertisements