PHP - Ds\Queue::toArray() Function



The PHPDs\Queue::toArray()function is used to convert the current queue into an array. An array is a linear data structure containing multiple data of the same data types. Once this function is invoked, the queue will be converted into an array that you can verify in the example output.

Syntax

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

public array Ds\Queue::toArray( void )

Parameters

This function does not accept any parameters.

Return value

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

Example 1

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

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

Output

The above program produces the following output −

The original queue is:
Ds\Queue Object
(
    [0] => 10
    [1] => 20
    [2] => 30
    [3] => 40
    [4] => 50
)
An array is:
Array
(
    [0] => 10
    [1] => 20
    [2] => 30
    [3] => 40
    [4] => 50
)

Example 2

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

<?php  
   $queue = new \Ds\Queue(["Tutorials", "Point", "India"]);
   echo "The original queue is: \n";
   print_r($queue);
   #using toArray() function
   $arr = $queue->toArray();
   echo "An array is: \n";
   print_r($arr);
?>

Output

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

The original queue is:
Ds\Queue Object
(
    [0] => Tutorials
    [1] => Point
    [2] => India
)
An array is:
Array
(
    [0] => Tutorials
    [1] => Point
    [2] => India
)
php_function_reference.htm
Advertisements