Syntax:public DsMap::slice (int $index, int $length)
Parameter:this the function takes two parameters, as described below:
- $index:This parameter specifies the index from which the elements will be returned in the subset. This parameter can be either positive or negative. If the $index is positive, then the range is calculated from the beginning of the map, and if it is negative, the index is positioned from the end.
- $length:This parameter is optional. If length $is not specified, then the elements in the subset will be from the start index to the end of the map. If length is specified, then the returned subset will contain elements starting at $index in the Map and ending with the specified length. For example, if $index = 2 and $length = 4, then the subset will contain 4 elements, starting with the element represented at index 2 in the actual Map instance.
Returned value: The Program 1:
// PHP program to illustrate the slice() function
$map
=
new
DsMap ([1 = > 10, 2 = > 20, 3 = > 30,
4 = > 40.5 = > 5 0.6 = > 60]);
// when the index is positive and the length is not
// specified
print_r (
$map
-> slice (4));
// when the index is negative and the length is not
// specified
print_r (
$map
-> slice (-4));
?>
Output:DsMap Object ([0] = > DsPair Object ([key] = > 5 [value] = > 50) [1] = > DsPair Object ([key] = > 6 [value] = > 60)) DsMap Object ([0] = > DsPair Object ([key] = > 3 [value] = > 30) [1] = > DsPair Object ([key] = > 4 [ value] = > 40) [2] = > DsPair Object ([key] = > 5 [value] = > 50) [3] = > DsPair Object ([key] = > 6 [value] = > 60))
Program 2:
// PHP program to illustrate the slice() function
$map
=
new
DsMap ([1 = > 10, 2 = > 20, 3 = > 30,
4 = > 40.5 = > 50.6 = > 60]);
// when the index is positive and the length
// specified
print_r (
$map
-> slice (2, 2));
// when the index is negative and the length
// specified
print_r (
$map
-> slice (-2, 2));
?>
Output:DsMap Object ([0] = > DsPair Object ([key] = > 3 [value] = > 30) [1] = > DsPair Object ([key] = > 4 [value] = > 40)) DsMap Object ([0] = > DsPair Object ([key] = > 5 [value] = > 50) [1] = > DsPair Object ([key] = > 6 [ value] = > 60))
Link : http://php.net/manual/en/ds-map.slice.php