PHP | Ds\Vector slice() Function
The Ds\Vector::slice() function is an inbuilt function in PHP which is used to return the sub-vector of given vector.
Syntax:
public Ds\Vector::slice( $index, $length ) : Ds\Vector
Parameters: This function accepts two parameter as mentioned above and described below:
- $index: This parameter hold the starting index of sub vector. The index value can be positive and negative. If the index value is positive then it starts at the index of vector and if index value is negative then it starts from ends.
- $length: This parameter holds the length of sub vector. This parameter can take positive and negative values. If length is positive then sub vector size is equal to given length and if length is negative then vector will stop that many values from the end.
Return Value: This function returns a sub-vector of given range.
Below programs illustrate the Ds\Vector::slice() function in PHP:
Program 1:
<?php // Create new vector $vect = new \Ds\Vector([1, 2, 3, 4, 5, 6]); echo("Original vector:\n"); // Display the vector element var_dump($vect); // Use slice() function to // create sub vector $res = $vect->slice(1, 2); echo("\nNew sub-vector\n"); // Display the sub-vector elements var_dump($res); ?> |
Output:
Original vector:
object(Ds\Vector)#1 (6) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
[3]=>
int(4)
[4]=>
int(5)
[5]=>
int(6)
}
New sub-vector
object(Ds\Vector)#2 (2) {
[0]=>
int(2)
[1]=>
int(3)
}
Program 2:
<?php // Create new vector $vect = new \Ds\Vector([1, 2, 3, 4, 5, 6]); echo("Original vector:\n"); // Display the vector element var_dump($vect); // Use slice() function to // create sub vector $res = $vect->slice(2, -2); echo("\nNew sub-vector\n"); // Display the sub-vector elements var_dump($res); $res = $vect->slice(4); echo("\nNew sub-vector\n"); // Display the sub-vector elements var_dump($res); ?> |
Output:
Original vector:
object(Ds\Vector)#1 (6) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
[3]=>
int(4)
[4]=>
int(5)
[5]=>
int(6)
}
New sub-vector
object(Ds\Vector)#2 (2) {
[0]=>
int(3)
[1]=>
int(4)
}
New sub-vector
object(Ds\Vector)#3 (2) {
[0]=>
int(5)
[1]=>
int(6)
}
Reference: http://php.net/manual/en/ds-vector.slice.php
Recommended Posts:
- PHP | Ds\Set slice() Function
- PHP Ds\Map slice() Function
- PHP | Ds\Deque slice() Function
- PHP | Ds\Sequence slice() Function
- CSS | border-image-slice Property
- JavaScript | Difference between String.slice and String.substring
- How to get the function name from within that function using JavaScript ?
- CSS | rgb() Function
- PHP | end() Function
- D3.js | d3.map.set() Function
- p5.js | second() function
- D3.js | d3.map.has() Function
- D3.js | d3.map.get() Function
- PHP | Ds\Set add() Function
- PHP | Ds\Map xor() 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.



