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

PHP - Ds Map::toArray() Function



The PHP Ds\Map::toArray() function is used to convert a map to an array. This function returns the converted array containing all the map elements in the same order as a map.

In PHP, an array is a data structure that allows you to store multiple values in a single variable.

Syntax

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

public Ds\Map::toArray(): array

Parameters

This function does not accept any parameter.

Return value

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

Example 1

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

<?php  
   $map = new \Ds\Map([10, 20, 30, 40, 50]);
   echo "The map elements are: \n";
   print_r($map);
   echo "An array elements are: \n";
   #using toArray() function
   print_r($map->toArray());
?>

Output

The above program produces the following output −

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

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

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

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

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

)
An array elements are:
Array
(
    [0] => 10
    [1] => 20
    [2] => 30
    [3] => 40
    [4] => 50
)

Example 2

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

<?php  
   $map = new \Ds\Map(["Tutorials", "Point", "India"]);
   echo "The map elements are: \n";
   print_r($map);
   echo "An array elements are: \n";
   #using toArray() function
   print_r($map->toArray()); 
?>

Output

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

The map elements 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
        )

)
An array elements are:
Array
(
    [0] => Tutorials
    [1] => Point
    [2] => India
)

Example 3

Maps with non-scalar keys cannot be converted to an array. While converting, this function will throw an error &minus.

<?php  
   $map = new \Ds\Map([]);
   $map->put(1, 'a');
   #adding non-scalar value
   $map->put([1, 2, 3], 'e');
   echo "The map elements are: \n";
   print_r($map);
   echo "An array elements are: \n";
   #using toArray() function
   print_r($map->toArray()); 
?>

Output

Once the above program is executed, it will throw the following error −

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

    [1] => Ds\Pair Object
        (
            [key] => Array
                (
                    [0] => 1
                    [1] => 2
                    [2] => 3
                )

            [value] => e
        )

)
An array elements are:
PHP Fatal error:  Uncaught TypeError: 
Cannot access offset of type array on array in C:\Apache24\htdocs\index.php:10
Stack trace:
#0 C:\Apache24\htdocs\index.php(10): Ds\Map->toArray()
#1 {main}
  thrown in C:\Apache24\htdocs\index.php on line 10
php_function_reference.htm
Advertisements