PHP Reference ยท Standard PHP Library and Classes
The SplFixedArray class
Reference for PHP SplFixedArray: a fixed-size array with integer keys, creating, resizing, converting to and from arrays, out-of-range errors and memory use.
SplFixedArray is an array with a fixed length and integer indexes starting at 0. Every slot exists from the start and holds null until you assign it. Because it needs no hash table and no key storage, it uses less memory than a regular array for large numeric lists, and it rejects any index outside its range.
For everyday code an ordinary array is simpler and has far more functions. Reach for SplFixedArray when you hold many thousands of items and memory matters, or when you want the array's size to be a hard rule.
<?php $grid = new SplFixedArray(3); $grid[0] = 'x'; echo $grid->getSize(), ' ', var_export($grid[1], true);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Syntax
class SplFixedArray implements IteratorAggregate, ArrayAccess, Countable, JsonSerializable { public function __construct(int $size = 0); public static function fromArray(array $array, bool $preserveKeys = true): SplFixedArray; }
| Member | Description |
|---|---|
$size |
Number of slots; must be zero or more. |
fromArray($array, $preserveKeys) |
Builds an SplFixedArray. With true, keys must be non-negative integers and gaps become null; with false, the values are re-indexed from 0. |
toArray() |
Returns a regular array. |
getSize() / count() |
The number of slots. |
setSize($size) |
Changes the size; new slots are null, removed slots are lost. |
$a[$i] |
Reads or writes a slot. |
Return value: toArray() returns an array; fromArray() returns the new object.
Errors / Exceptions: an index that is out of range throws OutOfBoundsException. A negative size throws ValueError; a key that is not a non-negative integer in fromArray() throws InvalidArgumentException.
Usage notes
- Fixed means fixed. Appending with
$a[] = xis not allowed; callsetSize()first, which is relatively costly. - Memory. For big lists of integers or floats it is smaller than a packed array; for small lists the difference is negligible. Measure with
memory_get_usage()before switching. - Speed. Reading and writing is about as fast as a packed array; many array functions need a
toArray()conversion first, which costs a copy. - Iteration is safe for nested loops. Since PHP 8.0 the class is an
IteratorAggregate, so two loops over the same object do not interfere with each other. - Values are not typed. Each slot can hold anything; the class does not enforce an element type.
- Objects are handles. Assigning an
SplFixedArraycopies the handle, not the data; useclonefor a real copy. - JSON.
json_encode()produces a list. - Not for sparse data. A fixed array of one million slots allocates them all, even if you use ten.