PHP - Ds Deque::toArray() Function
The PHPDs\Deque::toArray()function is used to convert the current deque to an array. The deque may contain any number of different elements.
This function converts the entire deque to an array and returns the array as the final result. The order of the elements will be the same as when they were added to the deque.
Syntax
Following is the syntax of the PHP Ds\Deque::toArray() function −
public Ds\Deque::toArray(): array
Parameters
This function does not accept any parameter.
Return value
This function returns an array containing all the values in the same order as the deque.
Example 1
The following program demonstrates the usage of the PHP Ds\Deque::toArray() function −
<?php $deque = new \Ds\Deque([36, 18, 9, 27, 45]); echo "The deque elements are: \n"; print_r($deque); echo "An array elements are: \n"; print_r($deque->toArray()); ?>
Output
After executing the above program, it will display the following output −
The deque elements are:
Ds\Deque Object
(
[0] => 36
[1] => 18
[2] => 9
[3] => 27
[4] => 45
)
An array elements are:
Array
(
[0] => 36
[1] => 18
[2] => 9
[3] => 27
[4] => 45
)
Example 2
Following is an example of the PHP Ds\Deque::toArray() function. This function is used to retrieve an array containing all elements of the deque (['b', 'd', 'h', 'k', 'l', 't']) −
<?php $deque = new \Ds\Deque(['b', 'd', 'h', 'k', 'l', 't']); echo "The deque elements are: \n"; print_r($deque); echo "The array elements are: \n"; print_r($deque->toArray()); ?>
Output
The above program generates the following output −
The deque elements are:
Ds\Deque Object
(
[0] => b
[1] => d
[2] => h
[3] => k
[4] => l
[5] => t
)
The array elements are:
Array
(
[0] => b
[1] => d
[2] => h
[3] => k
[4] => l
[5] => t
)