The Ds\Set::slice() function is an inbuilt function in PHP which is used to return the sub-set of given range.
Syntax:
Ds\Set public Ds\Set::slice ( int $index [, int $length ] )
Parameters: This function accepts two parameters as mentioned above and described below:
- $index: This parameter holds the starting index of sub-set. The index value can be positive and negative. If the index value is positive then it starts at the index of the set and if the index value is negative then set starts from ends.
- $length: This parameter holds the length of sub-set. This parameter can take positive and negative values. If length is positive then sub-set size is equal to a given length and if the length is negative then the set will stop that many values from the end.
Return Value: This function returns the sub-set of given range.
Below programs illustrate the Ds\Set::slice() function in PHP:
Program 1:
<?php // Create new set $set = new \Ds\Set([1, 3, 6, 9, 10, 15, 20]); // Use slice() function to create // sub-set and display it print_r($set->slice(2)); print_r($set->slice(1, 2)); print_r($set->slice(2, -2)); ?> |
Ds\Set Object
(
[0] => 6
[1] => 9
[2] => 10
[3] => 15
[4] => 20
)
Ds\Set Object
(
[0] => 3
[1] => 6
)
Ds\Set Object
(
[0] => 6
[1] => 9
[2] => 10
)
Program 2:
<?php // Create new set $set = new \Ds\Set(["Geeks", "GFG", "Abc", "for"]); // Use slice() function to create // sub-set and display it print_r($set->slice(3)); print_r($set->slice(2, 0)); print_r($set->slice(0, 3)); ?> |
Ds\Set Object
(
[0] => for
)
Ds\Set Object
(
)
Ds\Set Object
(
[0] => Geeks
[1] => GFG
[2] => Abc
)
Reference: https://www.php.net/manual/en/ds-set.slice.php
Recommended Posts:
- PHP | Ds\Sequence slice() Function
- PHP | Ds\Vector slice() Function
- PHP Ds\Map slice() Function
- PHP | Ds\Deque slice() Function
- Node.js | slice() function
- Nodejs lodash.slice() function
- JavaScript Array slice() Method
- JavaScript string.slice() Method
- CSS | border-image-slice Property
- JavaScript | Difference between String.slice and String.substring
- Node.js | Buffer.slice() Method
- What is the difference between Array.slice() and Array.splice() in JavaScript ?
- TypeScript | String slice() Method with example
- TypeScript | Array slice() Method
- Underscore.js _.iterators.slice() Method
- How to slice a string in AngularJS ?
- How to get the function name inside a function in PHP ?
- PHP 5 vs PHP 7
- PHP | Get PHP configuration information using phpinfo()
- PHP | php.ini File Configuration
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.

