• PHP Video Tutorials

PHP - Ds\Collection::toArray() Function



The PHP Ds\Collection::toArray() function is used to convert the current collection into an array and returns the collection elements in an array format. Whatever the initial input such as set, deck, stack, vector, etc., this function converts them directly into an array format.

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

Syntax

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

abstract public array Ds\Collection::toArray( void )

Parameters

This function does not accept any parameter.

Return value

This function returns the elements of the current collection in array format.

Example 1

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

<?php  
   $collection = new \Ds\Vector([10, 15, 20, 25, 30]); 
   echo "The vector elements are: \n";
   print_r($collection);
   #using the toArray() function
   echo "An array elements are: \n";
   var_dump($collection->toArray());
?>

Output

The above program produces the following output −

The vector elements are:
Ds\Vector Object
(
    [0] => 10
    [1] => 15
    [2] => 20
    [3] => 25
    [4] => 30
)
An array elements are:
array(5) {
  [0]=>
  int(10)
  [1]=>
  int(15)
  [2]=>
  int(20)
  [3]=>
  int(25)
  [4]=>
  int(30)
}

Example 2

Following is another example of the PHP Ds\Collection::toArray() function. We use this function to convert the current collection into an array format −

<?php  
   $set = new \Ds\Set();
   $set->add('Sunday');
   $set->add('Monday');
   $set->add('Tuesday');
   $set->add('Wednesday');
   $set->add('Thursday');
   $set->add('Friday');
   $set->add('Saturday');
   echo "The set elements are: \n";
   print_r($set);
   #using toArray() function
   $res=$set->toArray();
   echo "An array elements are: \n";
   print_r($res);
?>

Output

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

The set elements are:
Ds\Set Object
(
    [0] => Sunday
    [1] => Monday
    [2] => Tuesday
    [3] => Wednesday
    [4] => Thursday
    [5] => Friday
    [6] => Saturday
)
An array elements are:
Array
(
    [0] => Sunday
    [1] => Monday
    [2] => Tuesday
    [3] => Wednesday
    [4] => Thursday
    [5] => Friday
    [6] => Saturday
)

Example 3

In the below example, we use the PHP Ds\Collection::toArray() function to convert the current collection into an array format −

<?php  
   $seq = new \Ds\Vector(["test_string", 1525, false]);
   echo "The vector elements are: \n";
   var_dump($seq);
   #using toArray() function
   $res = $seq->toArray();
   echo "An array elements are: \n";
   var_dump($res);
?>

Output

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

The vector elements are:
object(Ds\Vector)#1 (3) {
  [0]=>
  string(11) "test_string"
  [1]=>
  int(1525)
  [2]=>
  bool(false)
}
An array elements are:
array(3) {
  [0]=>
  string(11) "test_string"
  [1]=>
  int(1525)
  [2]=>
  bool(false)
}

Example 4

Let's create another collection named stack([]) and use the same toArray() function to convert it into the array format −

<?php  
   $stack = new \Ds\Stack();
   $stack->push(2412);
   $stack->push(1262);
   $stack->push(457, 57, 58584);
   $stack->push(...[558, 6566]);
   echo "Stack elements are: \n";
   print_r($stack);
   #using toArray() function
   $res=$stack->toArray();
   echo "An array elements are: \n";
   print_r($res);  
?>

Output

On executing the above program, it displays the following output −

Stack elements are:
Ds\Stack Object
(
    [0] => 6566
    [1] => 558
    [2] => 58584
    [3] => 57
    [4] => 457
    [5] => 1262
    [6] => 2412
)
An array elements are:
Array
(
    [0] => 6566
    [1] => 558
    [2] => 58584
    [3] => 57
    [4] => 457
    [5] => 1262
    [6] => 2412
)
php_function_reference.htm
Advertisements