Syntax:DsSet public DsSet::slice (int $index [, int $length])
Parameters: This function takes two parameters as above and described below:
- $index:this parameter contains the starting index of the subset. The index value can be positive or negative. If the index value is positive, then it starts at the index of the set, and if the index value is negative, then the set starts at the end.
- $length:This parameter contains the length of the subset. This parameter can take positive and negative values. If the length is positive, then the size of the subset is equal to the given length, and if the length is negative, then the set will stop so many values from the end.
Return value:This function returns a
subset of the given range.The following programs illustrate the
Ds / Set::slice()function in PHP:
Program 1:
// Create a new set
$set
=
new
DsSet ([1, 3, 6, 9, 10, 15, 20]);
// Use the slice() function to create
// subset and display it
print_r (
$set
-> slice (2));
print_r (
$set
-> slice (1, 2));
print_r (
$set
-> slice (2, -2));
?>
Exit:DsSet Object ( [0] = > 6 [1] = > 9 [2] = > 10 [3] = > 15 [4] = > 20) DsSet Object ([0] = > 3 [1] = > 6) DsSet Object ([0] = > 6 [1] = > 9 [2] = > 10)
Program 2:
// Create a new set
$set
=
new
DsSet ([
"Geeks"
,
"GFG"
,
"Abc"
,
"for"
]);
// Use the slice() function to create
// subset and display it
print_r (
$set
-> slice (3));
print_r (
$set
-> slice (2, 0));
print_r (
$set
-> slice (0, 3));
?>
Exit:DsSet Object ( [0] = > for) DsSet Object() DsSet Object ([0] = > Geeks [1] = > GFG [2] = > Abc)
Link: https://www.php.net/manual/en/ds-set.slice.php