Syntax:DsSequence abstract public DsSequence::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 subsequence. The index value can be positive or negative. If the index value is positive, then it starts at the index of the sequence, and if the index value is negative, then the sequence starts at the end.
- $length:This parameter contains the length of the subsequence. This parameter can take positive and negative values. If the length is positive, then the size of the subsequence is equal to the specified length, and if the length is negative, the sequence will stop that many values from the end.
Return value:This function returns a subsequence of the specified range .The following programs illustrate the
Ds / Sequence::slice()function in PHP:
Program 1:
// Create a new sequence
$seq
=
new
DsVector ([1, 3, 6, 9, 10, 15, 20]);
// Use the slice() function to create
// subsequence and display it
print_r (
$seq
-> slice (2));
print_r (
$seq
-> slice (1, 2));
print_r (
$seq
-> slice (2, -2));
?>
Output:DsVector Object ([0] = > 6 [1] = > 9 [2] = > 10 [3] = > 15 [4] = > 20) DsVector Object ([0] = > 3 [1] = > 6) DsVector Object ([0] = > 6 [1] = > 9 [2] = > 10)
Program 2:
// Create a new sequence
$seq
=
new
DsVector ([
"Geeks"
,
"GFG"
,
"Abc"
,
"for"
]);
// Use the slice() function to create
// subsequence and display it
print_r (
$seq
-> slice (3));
print_r (
$seq
-> slice (2, 0));
print_r (
$seq
-> slice (0, 3));
?>
Output:DsVector Object ([0] = > for) DsVector Object() DsVector Object ([0] = > Geeks [1] = > GFG [2] = > Abc)
Link: http://php.net/manual/en/ds-sequence. slice.php