PHP - Ds\Pair::toArray() Function



The PHP Ds\Pair::toArray() function is used to convert a pair into an array. The element order in the retrieved array will be the same as in the pair.

In PHP, an array is a special variable that can hold many values under a single name, and you can access the values by referring to an index number or name.

Syntax

Following is the syntax of the PHP Ds\Pair::toArray() function −

public array Ds\Pair::toArray( )

Parameters

This function does not accept any parameters.

Return value

This function returns an array containing all values in the same order as the pair.

Example 1

The following program demonstrates the usage of the PHP Ds\Pair::toArray() function −

<?php  
   $pair = new \Ds\Pair(["a", "b"], [1, 2]);
   echo "The original pair: \n";
   print_r($pair);
   echo "The array is: \n";
   #using toArray() function   
   print_r($pair->toArray());  
?>

Output

The above program produces the following output −

The original pair:
Ds\Pair Object
(
    [key] => Array
        (
            [0] => a
            [1] => b
        )

    [value] => Array
        (
            [0] => 1
            [1] => 2
        )

)
The array is:
Array
(
    [key] => Array
        (
            [0] => a
            [1] => b
        )

    [value] => Array
        (
            [0] => 1
            [1] => 2
        )

)

Example 2

Following is another example of the PHP Ds\Pair::toArray() function. We use this function to convert this pair ([1, 2], ["Tutorials", "Point"]) into an array −

<?php  
   $pair = new \Ds\Pair([1, 2], ["Tutorials", "Point"]);
   echo "The original pair: \n";
   print_r($pair);
   echo "The array is: \n";
   #using toArray() function   
   print_r($pair->toArray());  
?>

Output

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

The original pair:
Ds\Pair Object
(
    [key] => Array
        (
            [0] => 1
            [1] => 2
        )

    [value] => Array
        (
            [0] => Tutorials
            [1] => Point
        )

)
The array is:
Array
(
    [key] => Array
        (
            [0] => 1
            [1] => 2
        )

    [value] => Array
        (
            [0] => Tutorials
            [1] => Point
        )

)
php_function_reference.htm
Advertisements