PHP - Ds Vector::toArray() Function
The PHP Ds\Vector::toArray() function is used to convert the vector to an array.
This function does not modify the current vector and returns an array, and the order of elements in arrays is the same as the vector.
Syntax
Following is the syntax of the PHP Ds\Vector::toArray() function −
public Ds\Vector::toArray(): array
Parameters
This function does not accept any parameter.
Return value
This function returns an array containing all values in the same order as vector.
Example 1
The following program demonstrates the usage of the PHP Ds\Vector::toArray() function −
<?php
$vector = new \Ds\Vector([1, 2, 3, 4, 5]);
echo("The original vector: \n");
print_r($vector);
echo("An array elements: \n");
print_r($vector->toArray());
?>
Output
After executing the above program, the following output will be displayed −
The original vector:
Ds\Vector Object
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
An array elements:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
Example 2
Following is another example of the PHP Ds\Vector::toArray() function. We use this function to convert this vector in an array (["Tutorials", "Point", "Tutorix"]) −
<?php
$vector = new \Ds\Vector(["Tutorials", "Point", "Tutorix"]);
echo("The original vector: \n");
print_r($vector);
echo("An array elements: \n");
print_r($vector->toArray());
?>
Output
The above program produces the following output −
The original vector:
Ds\Vector Object
(
[0] => Tutorials
[1] => Point
[2] => Tutorix
)
An array elements:
Array
(
[0] => Tutorials
[1] => Point
[2] => Tutorix
)