PHP | Ds\Deque toArray() Function
The Ds\Deque::toArray() function is an inbuilt function in PHP which is used to convert a Deque into an array. The elements of the array will be in the same order as they are in Deque.
Syntax:
public Ds\Deque::toArray( void ) : array
Parameters: This function does not accept any parameter.
Return Value: This function returns the array which contains all the elements of the Deque in the same order.
Below programs illustrate the Ds\Deque::toArray() function in PHP:
Program 1:
<?php // Declare a deque$deck = new \Ds\Deque([5, 6, 3, 2, 7, 1]); echo("Elements of Deque\n"); // Display the Deque elementsprint_r($deck); echo("Array elements\n"); // Use toArray() function to// convert Deque into arrayprint_r($deck->toArray()); ?> |
Output:
Elements of Deque
Ds\Deque Object
(
[0] => 5
[1] => 6
[2] => 3
[3] => 2
[4] => 7
[5] => 1
)
Array elements
Array
(
[0] => 5
[1] => 6
[2] => 3
[3] => 2
[4] => 7
[5] => 1
)
Program 2:
<?php // Declare a deque$deck = new \Ds\Deque(["geeks", "for", "geeks", "data structures"]); echo("Elements of Deque\n"); // Display the Deque elementsvar_dump($deck); echo("Array elements\n"); // Use toArray() function to// convert Deque into arrayvar_dump($deck->toArray()); ?> |
Output:
Elements of Deque
object(Ds\Deque)#1 (4) {
[0]=>
string(5) "geeks"
[1]=>
string(3) "for"
[2]=>
string(5) "geeks"
[3]=>
string(15) "data structures"
}
Array elements
array(4) {
[0]=>
string(5) "geeks"
[1]=>
string(3) "for"
[2]=>
string(5) "geeks"
[3]=>
string(15) "data structures"
}


