Open In App

PHP | Ds\Deque toArray() Function

Improve
Improve
Like Article
Like
Save
Share
Report

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 elements
print_r($deck);
  
echo("Array elements\n");
  
// Use toArray() function to
// convert Deque into array
print_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 elements
var_dump($deck);
  
echo("Array elements\n");
  
// Use toArray() function to
// convert Deque into array
var_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"
}

Reference: http://php.net/manual/en/ds-deque.toarray.php



Last Updated : 14 Aug, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads