From the course: Learning the Standard PHP Library

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Speeding up array access with SplFixedArray

Speeding up array access with SplFixedArray - PHP Tutorial

From the course: Learning the Standard PHP Library

Start my 1-month free trial

Speeding up array access with SplFixedArray

- According to the PHP online documentation, the SplFixedArray class is much faster than an ordinary array. It also uses less memory. But these advantages come at a price as the name suggests the size of the array is a fixed length and it allows only integers within that range as indexes. You can't use it for associative arrays. Although the class has public methods that allow you set, unset, and get elements, there are no methods to sort them. Another restriction that might not be immediately obvious from the documentation, is that SplFixedArray creates an object not an array. As a result, you can't use it with PHP's huge number of array manipulation functions. So, let's take a look at a bit of code. This is fixed_array.php which you can find in the chapter seven folder of the exercise files. It contains an ordinary array called animals. To convert this to an SplFixedArray, you need to use the from array method which is a static method of the SplFixedArray class. So, let's just add a…

Contents