• PHP Video Tutorials

PHP - Ds Set::toArray() Function



The PHP Ds\Set::toArray() function is used to convert the object or a collection (say set) into an array.

This function does not modify the current set and returns the converted array that has the values of the set without changing the order of the values.

Syntax

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

public array Ds\Set::toArray( void )

Parameters

This function does not accept any parameter.

Return value

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

Example 1

The following is the basic example of the PHP Ds\Set::toArray() function.

<?php  
   $set = new \Ds\Set([1, 2, 3, 4, 5]); 
   echo "The set elements are: \n";
   print_r($set);
   echo "The array is: \n"; 
   print_r($set->toArray()); 
?> 

Output

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

The set elements are:
Ds\Set Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)
The array is:
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)

Example 2

The following is another example of the PHP Ds\Set::toArray() function. We use this function to create an array containing all values in the same order as this set (["Tutorials", "Point", "India"]).

<?php  
   $set = new \Ds\Set(["Tutorials", "Point", "India"]); 
   echo "The set elements are: \n";
   print_r($set);
   echo "The array is: \n"; 
   $arr = $set->toArray();
   print_r($arr);
?>

Output

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

The set elements are:
Ds\Set Object
(
    [0] => Tutorials
    [1] => Point
    [2] => India
)
The array is:
Array
(
    [0] => Tutorials
    [1] => Point
    [2] => India
)
php_function_reference.htm
Advertisements