PHP | Ds\Vector remove() Function

The Ds\Vector::remove() function is an inbuilt function in PHP which is used to remove the value from index and return it.

Syntax:

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

Parameters: This function accepts single parameter $index which holds the index at which the element is to be removed.



Return Value: This function returns the value that was removed.

Exception: This function throws OutOfRangeException if the given index is out of range or not valid.

Below programs illustrate the Ds\Vector::remove() function in PHP:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Declare new vector
$vect = new \Ds\Vector(["geeks", "for",
            "geeks", "DataStructures"]);
  
echo("Vector Elements\n");
  
// Display the Vector elements
print_r($vect);
  
echo("\nLast element of the vector: \n");
  
// Use remove() function to remove 
// value at index 3 and return it
var_dump($vect->remove(3));
  
?>

chevron_right


Output:

Vector Elements
Ds\Vector Object
(
    [0] => geeks
    [1] => for
    [2] => geeks
    [3] => DataStructures
)

Last element of the vector: 
string(14) "DataStructures"

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Declare new vector
$vect = new \Ds\Vector([1, 2, 3, 4, 5, 6]);
  
echo("Vector Elements\n");
  
// Display the Vector elements
print_r($vect);
  
echo("\nLast element of the vector: \n");
  
// Use remove() function to remove 
// value at index 3 and return it
var_dump($vect->remove(3));
  
?>

chevron_right


Output:

Vector Elements
Ds\Vector Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
)

Last element of the vector: 
int(4)

Reference: http://php.net/manual/en/ds-vector.remove.php



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.