PHP | Ds\Vector set() Function

The Ds\Vector::set() function is an inbuilt function in PHP which is used to set the value in the vector at given index.

Syntax:

public Ds\Vector::set( $index, $value ) : void

Parameters: This function accepts two parameters as mentioned above and described below:

  • $index: This parameter holds the index value at which the vector value to be updated.
  • $value: This parameter holds the value by which previous element is to be replaced.

Return Value: This function does not return any value.

Exception: This function throws OutOfRangeException if index is invalid.

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

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create new Vector
$vect = new \Ds\Vector([1, 2, 3, 4, 5]);
  
// Display the Vector elements
print_r($vect);
  
// Use set() function to set the 
// element in the vector
$vect->set(1, 10);
  
echo("\nVector after updating the element\n");
  
// Display the vector elements
print_r($vect);
  
?>

chevron_right


Output:

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

Vector after updating the element
Ds\Vector Object
(
    [0] => 1
    [1] => 10
    [2] => 3
    [3] => 4
    [4] => 5
)

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create new Vector
$vect = new \Ds\Vector(["geeks", "of", "geeks"]);
  
// Display the Vector elements
print_r($vect);
  
// Use set() function to set the 
// element in the vector
$vect->set(1, "for");
  
echo("\nVector after updating the element\n");
  
// Display the vector elements
print_r($vect);
  
?>

chevron_right


Output:

Ds\Vector Object
(
    [0] => geeks
    [1] => of
    [2] => geeks
)

Vector after updating the element
Ds\Vector Object
(
    [0] => geeks
    [1] => for
    [2] => geeks
)

Reference: http://php.net/manual/en/ds-vector.set.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.