The get()
method of PHP’s DS\Vector
class returns the value stored at a particular vector index.
The process is illustrated below:
The prototype of the get()
method is shown below:
mixed public Ds\Vector::get( int $index )
The get()
method takes a mandatory parameter, i.e., the vector index whose value is returned.
If the index is outside the valid range of indices for the vector, the get()
method throws the OutOfRangeException.
The get()
method returns the value at the specified vector index.
The code below shows how the get()
method works in PHP:
<?php// initialize vector$newVector = new \Ds\Vector([1, "abc", 10, 2, "efg"]);print_r($newVector);for ($i = 0; $i < 5; $i++){// get value at each index$value = $newVector->get($i);print("The value at index " . $i . " is " . $value . ".\n");}?>
The code above produces the following output:
Ds\Vector Object
(
[0] => 1
[1] => abc
[2] => 10
[3] => 2
[4] => efg
)
The value at index 0 is 1.
The value at index 1 is abc.
The value at index 2 is 10.
The value at index 3 is 2.
The value at index 4 is efg.
Note: To run this program, you need to install Data Structures for PHP on your local machine.
The vector newVector
is initialized with distinct values of different data types.
The for-loop
proceeds to iterate over each vector index. The get()
method in line is invoked during each loop iteration. Each sector value is fetched and output accordingly.
Free Resources