PHP | Ds\Vector insert() Function
The Ds\Vector::insert() function is an inbuilt function in PHP which is used to insert the element into the vector at the given index.
Syntax:
void public Ds\Vector::insert( $index, $values )
Parameters: This function accepts two parameter as mentioned above and described below:
- $index: This parameter holds the index at which element is to be inserted.
- $value: This parameter holds the value which is to be inserted.
Return Value: This function does not return any value.
Exception: This function returns OutOfRangeException if the index is not valid.
Below programs illustrate the Ds\Vector::insert() function in PHP:
Program 1:
<?php // Declare new vector $vector = new \Ds\Vector([1, 2, 4, 5]); // Display the Vector element print_r($vector); // Use insert() function to add // element in the vector $vector->insert(2, 3); // Display the vector element print_r($vector); ?> |
Output:
Ds\Vector Object
(
[0] => 1
[1] => 2
[2] => 4
[3] => 5
)
Ds\Vector Object
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
Program 2:
<?php // Declare new vector $vector = new \Ds\Vector(["geeks", "for", "geeks"]); // Display the Vector element print_r($vector); // Use insert() function to add // element in the vector $vector->insert(1, "practice"); // Display the vector element print_r($vector); ?> |
Output:
Ds\Vector Object
(
[0] => geeks
[1] => for
[2] => geeks
)
Ds\Vector Object
(
[0] => geeks
[1] => practice
[2] => for
[3] => geeks
)
Reference: http://php.net/manual/en/ds-vector.insert.php
Recommended Posts:
- PHP | Ds\Deque insert() Function
- PHP | Ds\Sequence insert() Function
- Insert string at specified position in PHP
- How to insert an item at the beginning of an array in PHP ?
- How to insert new row at a certain index in a table in jQuery ?
- How to insert line break before an element using CSS?
- How to insert spaces/tabs in text using HTML/CSS?
- How to insert a string at a specific index in JavaScript ?
- Program to Insert new item in array on any position in PHP
- How to insert HTML content into an iFrame using jQuery?
- What is the Efficient way to insert a number into a sorted array of numbers?
- Java Servlet and JDBC Example | Insert data in MySQL
- How to insert an item into array at specific index in JavaScript?
- How to call a function that return another function in JavaScript ?
- How to get the function name inside a function in PHP ?
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.



