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

PHP - Ds Vector::remove() Function



The PHP Ds\Vector::remove() function is used to remove a value in a vector at the specified index and returns the removed element in the result.

This function throws an OutOfRangeException if an index is not valid, which means the index value is "negative" or "exceeds" the vector size.

Syntax

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

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

Parameters

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

  • $index − The index of the values to remove.

Return value

This function returns a value that was removed.

Example 1

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

<?php
   $vector = new \Ds\Vector([1, 2, 3, 4, 5]);
   echo("The vector elements are: \n"); 
   print_r($vector);
   $index = 0;
   echo "The index value is: ".$index;
   echo "\nThe element removed at index ".$index." is: ";
   print_r($vector->remove($index));   
?>

Output

The above program produces the following output −

The vector elements are:
Ds\Vector Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)
The index value is: 0
The element removed at index 0 is: 1

Example 2

Following is another example of the PHP Ds\Vector::remove() function. We use this function to remove and retrieve the element at the specified index 2 of this vector (["Tutorials", "Point", "Tutorix"]) −

<?php 
   $vector = new \Ds\Vector(["Tutorials", "Point", "Tutorix"]);
   echo("The vector elements are: \n");
   print_r($vector);
   $index = 2;
   echo "The index is: ".$index;
   echo "\nThe removed element: \n";
   #using remove() function
   print_r($vector->remove($index));
   echo "\nThe updated vector: \n";
   print_r($vector);
?>

Output

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

The vector elements are:
Ds\Vector Object
(
    [0] => Tutorials
    [1] => Point
    [2] => Tutorix
)
The index is: 2
The removed element:
Tutorix
The updated vector:
Ds\Vector Object
(
    [0] => Tutorials
    [1] => Point
)

Example 3

If the specified index is invalid, the remove() function throws an "OutOfRangeException" as follows −

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

Output

Once the above program is executed, it will throw the following exception −

The vector elements are:
Ds\Vector Object
(
    [0] => a
    [1] => e
    [2] => i
    [3] => o
    [4] => u
)
The index is: -1
The removed element:
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