Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PHP - Ds Vector::slice() Function



The PHPDs\Vector::slice()function is used to retrieve a sub-vector of the given range from the original vector. The sub-vector is a part of the original vector. For example, vector {1, 2} is a sub-vector of this original vector {1, 2, 3}.

This function accepts an optional parameter named 'length'. Below is the list of key points about this parameter −

  • If the 'length' is negative, the vector stops that many elements from the end.
  • If the 'length' is positive, the sub-vector has that many elements in it.
  • If the 'length' exceeds the vector size, only values up to the end of the vector will be included.
  • If the 'length' is not provided, the sub-vector will contain all the values between the 'index' and the end of the vector.

Syntax

Following is the syntax of the PHP Ds\Vector::slice() function −

public Ds\Vector::slice(int $index, int $length = ?): Ds\Vector

Parameters

Following are the parameters of this function −

  • index − The index at which the sub-vector starts extracting.
  • length − The length of the sub-vector.

Return value

This function returns a sub-vector of the given range.

Example 1

Following is the basic example of the PHP Ds\Vector::slice() function −

<?php   
   $vector = new \Ds\Vector([1, 2, 3, 4, 5]);  
   echo("The original vector: \n");   
   print_r($vector);
   $index = 1;
   $length = 2;
   echo "The index and length is: ".$index.", ".$length;
   #using slice() function
   $result = $vector->slice(1, 2); 
   echo("\nThe new sub-vector:\n");  
   print_r($result);
?>

Output

The above program produces the following output −

The original vector:
Ds\Vector Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)
The index and length is: 1, 2
The new sub-vector:
Ds\Vector Object
(
    [0] => 2
    [1] => 3
)

Example 2

If the length is negative, the vector will stop that many values from the end.

Following is another example of the PHP Ds\Vector::slice() function. We use this function to retrieve a sub-vector from the original vector of the given range (1, -2) −

<?php 
   $vector = new \Ds\Vector([10, 20, 30, 40, 50]);
   echo("The original vector: \n");
   print_r($vector);
   $index = 1;
   $length = -2;
   echo "The index and length is: ".$index.", ".$length;
   $result = $vector->slice(1, -2);
   echo("\nThe new sub-vector: \n");
   print_r($result);
?>

Output

After executing the above program, the following output will be displayed −

The original vector:
Ds\Vector Object
(
    [0] => 10
    [1] => 20
    [2] => 30
    [3] => 40
    [4] => 50
)
The index and length is: 1, -2
The new sub-vector:
Ds\Vector Object
(
    [0] => 20
    [1] => 30
)

Example 3

If the length value exceeds the vector size, only values up to the end of the vector will be included.

<?php 
   $vector = new \Ds\Vector(["Tutorials", "Point", "India"]);
   echo("The original vector: \n");
   print_r($vector);
   $index = 0;
   $length = 10;
   echo "The index and length is: ".$index.", ".$length;
   $result = $vector->slice($index, $length);
   echo("\nThe new sub-vector: \n");
   print_r($result);
?>

Output

Once the above program is executed, it will display the following output −

The original vector:
Ds\Vector Object
(
    [0] => Tutorials
    [1] => Point
    [2] => India
)
The index and length is: 0, 10
The new sub-vector:
Ds\Vector Object
(
    [0] => Tutorials
    [1] => Point
    [2] => India
)

Example 4

If a length is not provided, the resulting vector will contain all values between the index and the end of the vector.

<?php 
   $vector = new \Ds\Vector(['a', 'e', 'i', 'o', 'u']);
   echo("The original vector: \n");
   print_r($vector);
   $index = 2;
   echo "The index is: ".$index;
   $result = $vector->slice($index);
   echo("\nThe new sub-vector: \n");
   print_r($result);
?>

Output

Following is the output of the above program −

The original vector:
Ds\Vector Object
(
    [0] => a
    [1] => e
    [2] => i
    [3] => o
    [4] => u
)
The index is: 2
The new sub-vector:
Ds\Vector Object
(
    [0] => i
    [1] => o
    [2] => u
)
php_function_reference.htm
Advertisements