Is there a lightweight replacement for PHP's array
to be used when I don't need any of the associated array functionality? From what I know, array
is a hash map internally, which is excessive and inefficient for storing a simple array of elements. If PHP had a class or programming construct similar to C++'s std::vector
, it would be just great.
Reference: http://www.cplusplus.com/reference/vector/vector/
PHP in TeluguThe Vector is a sequence of values in a contiguous buffer that grows and shrinks automatically. It is the most efficient sequential structure because the value index is a direct mapping to its index in a buffer, and the growth factor is not bound to specific multiple or exponent.
A rough equivalent of a C++ vector would be a resizing C array (to account for more elements than available). Ergo, the equivalent of an array of vectors would be an array of pointers (an array of arrays wouldn't cut it because of the resizing constraint). int* values[1000];
Difference between std::vector and std::array in C++Vector is a sequential container to store elements and not index based. Array stores a fixed-size sequential collection of elements of the same type and it is index based. Vector is dynamic in nature so, size increases with insertion of elements.
Here is a rule of thumb: If you want to add elements to your container or remove elements from your container, use a std::vector; if not, use a std::array.
Have a look at SPL datastructures. An example is http://www.php.net/manual/en/class.splfixedarray.php which is faster than normal array.
http://www.php.net/manual/en/spl.datastructures.php
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With