array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'es', ), 'this' => array ( 0 => 'class.splfixedarray.php', 1 => 'SplFixedArray', ), 'up' => array ( 0 => 'spl.datastructures.php', 1 => 'Estructuras de datos', ), 'prev' => array ( 0 => 'splpriorityqueue.valid.php', 1 => 'SplPriorityQueue::valid', ), 'next' => array ( 0 => 'splfixedarray.construct.php', 1 => 'SplFixedArray::__construct', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'es', 'path' => 'reference/spl/splfixedarray.xml', ), 'extra_header_links' => array ( 'rel' => 'alternate', 'href' => '/manual/en/feeds/class.splfixedarray.atom', 'type' => 'application/atom+xml', ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); ?>
(PHP 5 >= 5.3.0, PHP 7, PHP 8)
la clase SplFixedArray proporciona la funcionalidad principal de un array. La principal diferencia entre SplFixedArray y un array normal de PHP es que la clase SplFixedArray es de longitud fija y sólo permite enteros dentro del rango de índices. La ventaja es que usa menos memoría que un array estándar.
Versión | Descripción |
---|---|
8.2.0 | The SplFixedArray::__serialize() and SplFixedArray::__unserialize() magic methods have been added to SplFixedArray. |
8.1.0 | SplFixedArray implements JsonSerializable now. |
8.0.0 | SplFixedArray implements IteratorAggregate now. Previously, Iterator was implemented instead. |
Ejemplo #1 Ejemplo de uso SplFixedArray
<?php
// Inicializar el array con una longitud fija
$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"
// Aumentar el tamaño del array a 10
$array->setSize(10);
$array[9] = "asdf";
// Reducir el tamaño de un array a 2
$array->setSize(2);
// Las siguientes líneas lanzan una RuntimeException: Index invalid or out of range (Índice inválido o fuera de rango)
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";
}
?>
El resultado del ejemplo sería:
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