The SplFixedArray class
(PHP 5 >= 5.3.0, PHP 7)
Introduction
The SplFixedArray class provides the main functionalities of array. The main differences between a SplFixedArray and a normal PHP array is that the SplFixedArray is of fixed length and allows only integers within the range as indexes. The advantage is that it allows a faster array implementation.
Class synopsis
Examples
Example #1 SplFixedArray usage example
<?php
// Initialize the array with a fixed length
$array = new SplFixedArray(5);
$array[1] = 2;
$array[4] = "foo";
var_dump($array[0]); // NULL
var_dump($array[1]); // int(2)
var_dump($array["4"]); // string(3) "foo"
// Increase the size of the array to 10
$array->setSize(10);
$array[9] = "asdf";
// Shrink the array to a size of 2
$array->setSize(2);
// The following lines throw a RuntimeException: Index invalid or out of range
try {
var_dump($array["non-numeric"]);
} catch(RuntimeException $re) {
echo "RuntimeException: ".$re->getMessage()."\n";
}
try {
var_dump($array[-1]);
} catch(RuntimeException $re) {
echo "RuntimeException: ".$re->getMessage()."\n";
}
try {
var_dump($array[5]);
} catch(RuntimeException $re) {
echo "RuntimeException: ".$re->getMessage()."\n";
}
?>
The above example will output:
NULL int(2) string(3) "foo" RuntimeException: Index invalid or out of range RuntimeException: Index invalid or out of range RuntimeException: Index invalid or out of range
Table of Contents
- SplFixedArray::__construct — Constructs a new fixed array
- SplFixedArray::count — Returns the size of the array
- SplFixedArray::current — Return current array entry
- SplFixedArray::fromArray — Import a PHP array in a SplFixedArray instance
- SplFixedArray::getSize — Gets the size of the array
- SplFixedArray::key — Return current array index
- SplFixedArray::next — Move to next entry
- SplFixedArray::offsetExists — Returns whether the requested index exists
- SplFixedArray::offsetGet — Returns the value at the specified index
- SplFixedArray::offsetSet — Sets a new value at a specified index
- SplFixedArray::offsetUnset — Unsets the value at the specified $index
- SplFixedArray::rewind — Rewind iterator back to the start
- SplFixedArray::setSize — Change the size of an array
- SplFixedArray::toArray — Returns a PHP array from the fixed array
- SplFixedArray::valid — Check whether the array contains more elements
- SplFixedArray::__wakeup — Reinitialises the array after being unserialised
English translation
You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.
Thank you in advance.
Document created the 30/01/2003, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/php-rf-class.splfixedarray.html
The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.
References
These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.