What is the Ds\Set slice() function in PHP?

Overview

The Ds\Set::slice() function in PHP is used to return the subset of a given range.

Syntax

The slice() function can be declared as shown in the code snippet below:

Ds\Set public Ds\Set::slice ( int $ind [, int $len ] )
  • ind: This is the starting index of the subset.

Note: If ind is negative, then indexing of the set is started from the end of the set to its start.

  • len: This is the length of the subset.

Return value

The slice() function returns the subset of a given range.

Example

The code snippet below demonstrates the use of the slice() function:

<?php
$s1 = new \Ds\Set([5, 10, 15, 20, 25, 30]);
print_r($s1->slice(2, 4));
print_r($s1->slice(3));
?>

Explanation

  • Line 2: We declare a set s1.
  • Line 4: We use the slice() function to get a subset of s1 from index 0 of length 3.
  • Line 5: We use the slice() function to get a subset of s1 from index 3 until the end of the set s1.

Free Resources