PHP - DsMap::skip() Function



The PHP Ds\Map::skip() function is used to retrieve the pair at a given positional index, skipping the other map element pairs. The term "pair" refers to the key and value pair of the map elements.

If the given index position is not valid (i.e., it exceeds the length of the map), this function throws an "OutOfRangeException".

Syntax

Following is the syntax of the PHP Ds\Map::skip() function −

public Ds\Pair Ds\Map::skip( int $position )

Parameters

Following is the parameter of this function −

  • position − A zero-based position index to return.

Return value

This function returns the Ds\Pair at a given position.

Example 1

The following is the basic example of the PHP Ds\Map::skip() function −

<?php  
   $map = new \Ds\Map([1 => 10, 2 => 20, 3 => 30]);
   echo "The map values are: \n";
   print_r($map);
   $position = 1;
   echo "The position value: ".$position;
   echo "\nThe pair at position ".$position." is: \n";
   print_r($map->skip($position));
?>

Output

The above program produces the following output −

The map values are:
Ds\Map Object
(
    [0] => Ds\Pair Object
        (
            [key] => 1
            [value] => 10
        )

    [1] => Ds\Pair Object
        (
            [key] => 2
            [value] => 20
        )

    [2] => Ds\Pair Object
        (
            [key] => 3
            [value] => 30
        )

)
The position value: 1
The pair at position 1 is:
Ds\Pair Object
(
    [key] => 2
    [value] => 20
)

Example 2

Following is another example of the PHP Ds\Map::skip() function. We use this function to retrieve the Ds\Pair at a given position 0 of this map (["Tutorials", "Point", "India"]) −

<?php  
   $map = new \Ds\Map(["Tutorials", "Point", "India"]);
   echo "The map values are: \n";
   print_r($map);
   $position = 0;
   echo "The position value: ".$position;
   echo "\nThe pair at position ".$position." is: \n";
   print_r($map->skip($position));
?>

Output

After executing the above program, it will display the following output −

The map values are:
Ds\Map Object
(
    [0] => Ds\Pair Object
        (
            [key] => 0
            [value] => Tutorials
        )

    [1] => Ds\Pair Object
        (
            [key] => 1
            [value] => Point
        )

    [2] => Ds\Pair Object
        (
            [key] => 2
            [value] => India
        )

)
The position value: 0
The pair at position 0 is:
Ds\Pair Object
(
    [key] => 0
    [value] => Tutorials
)

Example 3

If the given position is not valid, this function throws an "OutOfRangeException" exception −

<?php  
   $map = new \Ds\Map(['a', 'e', 'i', 'o', 'u']);
   echo "The map values are: \n";
   print_r($map);
   $position = 5;
   echo "The position value: ".$position;
   echo "\nThe pair at position ".$position." is: \n";
   print_r($map->skip($position));
?>

Output

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

The map values are:
Ds\Map Object
(
    [0] => Ds\Pair Object
        (
            [key] => 0
            [value] => a
        )

    [1] => Ds\Pair Object
        (
            [key] => 1
            [value] => e
        )

    [2] => Ds\Pair Object
        (
            [key] => 2
            [value] => i
        )

    [3] => Ds\Pair Object
        (
            [key] => 3
            [value] => o
        )

    [4] => Ds\Pair Object
        (
            [key] => 4
            [value] => u
        )

)
The position value: 5
The pair at position 5 is:
PHP Fatal error:  Uncaught OutOfRangeException: 
Index out of range: 5, expected 0 <= x <= 4 in C:\Apache24\htdocs\index.php:8
Stack trace:
#0 C:\Apache24\htdocs\index.php(8): Ds\Map->skip(5)
#1 {main}
  thrown in C:\Apache24\htdocs\index.php on line 8
php_function_reference.htm
Advertisements