A Vector Data Structure is used to store the sequence of values in a contiguous memory buffer which grows and shrinks automatically. The vector data structure mapped the index value to its index buffer and the growth factor isn’t bound to a specific multiple or exponent. It is the efficient Data Structure in PHP 7 to provide the alternative of an array.
Requirements: PHP 7 is required for both extension and the compatibility polyfill.
Installation: The easiest way to install data structure by using PECL extension.
pecl install ds
Syntax:
public Ds\Vector:: Function
Example: Below programs illustrate the Ds\Vector::find() Function in PHP:
PHP
<?php // Declare a Vector elements $vector = new \Ds\Vector([1, 2, 3, 4, 5]); // Display the vector elements var_dump( $vector ); // Use find() function to find // the index value of element var_dump( $vector ->find(1)); var_dump( $vector ->find(5)); var_dump( $vector ->find(8)); var_dump( $vector ->find( "Geeks" )); ?> |
Output:
object(Ds\Vector)#1 (5) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(4) [4]=> int(5) } int(0) int(4) bool(false) bool(false)
The complete list of data structure DS\Vector are given below:
Functions |
Description |
---|---|
allocate() | It provides the custom size of the vector to allocate space. |
apply() | Update all values in the array by applying the callback function to each value of the vector. |
capacity() | Return the current capacity of the vector. |
clear() | Clear the vector elements by removing all the elements from |
__construct() | Create a new instance. |
contains() | Check the vector contains given value or not. |
copy() | Create a copy of given vector |
count() | Count the number of elements in the vector. |
filter() | Filter out the only elements which satisfy the condition defined in the callback function. |
find() | Find the index of the element in the vector. |
first() | Find the first element in the vector. |
get() | Return the element at the given index. |
insert() | Insert the element into the vector at the given index. |
isEmpty() | Check the vector is empty or not. |
join() | Separator provided as the argument. |
jsonSerialize() | Return the element which can be converted to JSON. |
last() | Return the last element of the vector. |
map() | Return the result of a callback after applying to each value in the vector. |
merge() | Merge all the elements to the vector. |
pop() | Remove the last element of a vector and return it. |
push() | Add elements to the end of the vector. |
reduce() | Reduce the vector to a single value by applying operations in the callback function. |
remove() | Remove and return a value by index. |
reverse() | Reverse the vector elements in-place. |
rotate() | Rotate the array elements by a given number of rotation. Rotations also happen in-place. |
set() | Set the value in the vector at the given index. |
shift() | Remove the first element from the vector and return it. |
slice() | Return the sub-vector of the given vector. |
sort() | Sort the elements of vector in-place. |
sorted() | This is used to sort the elements of the vector by creating a copy of the original vector. |
sum() | Returns the sum of all the elements of the vector. |
toArray() | All the elements of the vector are copied to the array. |
unshift() | This is used to adds elements to the front of vector. |