PHP | Ds\Vector unshift() Function
The Ds\Vector::unshift() function is an inbuilt function in PHP which is used to adds elements to the front of vector. This function move all the elements in the vector towards forward and adds the new element to the front.
Syntax:
public Ds\Vector::unshift( $values ) : void
Parameters: This function accepts single parameter $values which holds the values to be added to the vector.
Return Value: This function does not return any value.
Below programs illustrate the Ds\Vector::unshift() function in PHP:
Program 1:
<?php // Create new Vector $vect = new \Ds\Vector([3, 6, 1, 2, 9, 7]); echo("Original vector:\n"); // Display the vector elements print_r($vect); echo("\nArray elements after inserting new element\n"); // Use unshift() function to aff elements $vect->unshift(10, 20, 30); // Display updated vector elements print_r($vect); ?> |
Output:
Original vector:
Ds\Vector Object
(
[0] => 3
[1] => 6
[2] => 1
[3] => 2
[4] => 9
[5] => 7
)
Array elements after inserting new element
Ds\Vector Object
(
[0] => 10
[1] => 20
[2] => 30
[3] => 3
[4] => 6
[5] => 1
[6] => 2
[7] => 9
[8] => 7
)
Program 2:
<?php // Create new Vector $vect = new \Ds\Vector(["geeks", "for", "geeks"]); echo("Original vector:\n"); // Display the vector elements print_r($vect); echo("\nArray elements after inserting new element\n"); // Use unshift() function to aff elements $vect->unshift("PHP articles"); // Display updated vector elements print_r($vect); ?> |
Output:
Original vector:
Ds\Vector Object
(
[0] => geeks
[1] => for
[2] => geeks
)
Array elements after inserting new element
Ds\Vector Object
(
[0] => PHP articles
[1] => geeks
[2] => for
[3] => geeks
)
Reference: http://php.net/manual/en/ds-vector.unshift.php
Recommended Posts:
- PHP | Ds\Deque unshift() Function
- PHP | SplDoublyLinkedList unshift() Function
- PHP | Ds\Sequence unshift() Function
- How to get the function name from within that function using JavaScript ?
- p5.js | log() function
- D3.js | d3.map.set() Function
- PHP | each() Function
- p5.js | cos() function
- p5.js | sin() function
- p5.js | box() Function
- PHP | Ds\Map get() Function
- p5.js | hue() function
- p5.js | max() function
- p5.js | min() function
- D3.js | d3.sum() function
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.



