Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PHP - Ds Sequence::remove() Function



The PHP Ds\Sequence::remove() function removes a value at the specified index in a sequence and returns the removed value as a result.

If the specified index is invalid (i.e., the index is negative or greater than the sequence size), this function will throw an "OutOfRangeException".

Syntax

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

public abstract mixed Ds\Sequence::remove( int $index )

Parameters

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

  • index − The position at which the element will be removed.

Return value

This function returns a value that has been removed.

Example 1

The following program demonstrates the usage of the PHP Ds\Sequence::remove() function −

<?php 
   $seq = new \Ds\Vector([10, 20, 30]);
   echo "The sequence elements are: \n";
   print_r($seq); 
   $index = 0;
   echo "The index value is: ".$index;
   echo "\nThe removed value is: ";
   #using remove() function
   print_r($seq->remove($index));
?>

Output

The above program produces the following output −

The sequence elements are:
Ds\Vector Object
(
    [0] => 10
    [1] => 20
    [2] => 30
)
The index value is: 0
The removed value is: 10

Example 2

Following is another example of the PHP Ds\Sequence::remove() function. We use this function to remove the element at the specified index 3 in this sequence (['a', 'e', 'i', 'o', 'u']) −

<?php 
   $seq = new \Ds\Vector(['a', 'e', 'i', 'o', 'u']);
   echo "The sequence elements are: \n";
   print_r($seq); 
   $index = 3;
   echo "The index value is: ".$index;
   echo "\nThe removed value is: ";
   #using remove() function
   print_r($seq->remove($index));
   echo "\nThe sequence after remove: \n";
   print_r($seq); 
?>

Output

After executing the above program, the following output will be displayed −

The sequence elements are:
Ds\Vector Object
(
    [0] => a
    [1] => e
    [2] => i
    [3] => o
    [4] => u
)
The index value is: 3
The removed value is: o
The sequence after remove:
Ds\Vector Object
(
    [0] => a
    [1] => e
    [2] => i
    [3] => u
)

Example 3

If the given index is invalid, this function will throw an "OutOfRangeException" −

<?php 
   $seq = new \Ds\Vector(['a', 'e', 'i', 'o', 'u']);
   echo "The sequence elements are: \n";
   print_r($seq); 
   $index = -1;
   echo "The index value is: ".$index;
   echo "\nThe removed value is: ";
   #using remove() function
   print_r($seq->remove($index));
?>

Output

Once the above program is executed, it throws the following exception −

The sequence elements are:
Ds\Vector Object
(
    [0] => a
    [1] => e
    [2] => i
    [3] => o
    [4] => u
)
The index value is: -1
The removed value is: PHP Fatal error:  Uncaught OutOfRangeException: 
Index out of range: -1, expected 0 <= x <= 4 in C:\Apache24\htdocs\index.php:9
Stack trace:
#0 C:\Apache24\htdocs\index.php(9): Ds\Vector->remove(-1)
#1 {main}
  thrown in C:\Apache24\htdocs\index.php on line 9
php_function_reference.htm
Advertisements