CKTIS / PHP
SplFixedArray::toArray — Convert to a native PHP array
`toArray()` returns a native PHP array containing every slot, including those with a `null` value, without changing the original object.
Version: PHP 8.3
Syntax
public function toArray(): arraySplFixedArray::toArray is a public instance method of the SPL class \SplFixedArray. SPL is always enabled in standard PHP 8.3 builds. Call the method on an object with ->: $fixed->toArray(). It takes no arguments and returns an array, not a SplFixedArray object.
The result preserves the order and zero-based integer indexes. Slots you never assigned a value to are still present as null; the method does not skip them. A fixed array of size zero returns [].
Use this method when code expects an array or when you want to append with [] without resizing the original object. Replacing a scalar value or appending an element in the result does not change the source. This is not a deep copy: if an element holds an object, changing that object's properties through the result is visible through the source too.
Examples
<?php
$fixed = new \SplFixedArray(3);
$fixed[0] = 12;
$fixed[2] = 30;
$values = $fixed->toArray();
echo json_encode($values), "\n";
$values[0] = 99;
$values[] = 40;
echo json_encode($values), "\n";
echo json_encode($fixed->toArray()), "\n";
Result
[12,null,30] [99,null,30,40] [12,null,30]
The first line shows that index `1` is still `null`. The second shows the replacement at index `0` and the appended element. The third shows that the original object is unchanged. `json_encode()` is used only to display the result.
Related lessons
- PHP · Lesson 66 · Advanced · Version: 2