大网数据广告活动
PHP 语言与运行手册

The SplFixedArray class

· 52CE 我爱测

The SplFixedArray class provides the main functionalities of array. The main difference between a SplFixedArray and a normal PHP array is that the SplFixedArray must be resized manually and allows only integers within the range as indexes. The advantage is that it uses less memor

The SplFixedArray class

(PHP 5 >= 5.3.0, PHP 7, PHP 8)

简介

The SplFixedArray class provides the main functionalities of array. The main difference between a SplFixedArray and a normal PHP array is that the SplFixedArray must be resized manually and allows only integers within the range as indexes. The advantage is that it uses less memory than a standard array.

类摘要

class SplFixedArray implements IteratorAggregate, ArrayAccess, Countable, JsonSerializable {
/* 方法 */
public function __construct(int $size = 0)
public function count(): int
public function current(): mixed
public static function fromArray(array $array, bool $preserveKeys = true): SplFixedArray
public function getIterator(): Iterator
public function getSize(): int
public function jsonSerialize(): array
public function key(): int
public function next(): void
public function offsetExists(int $index): bool
public function offsetGet(int $index): mixed
public function offsetSet(int $index, mixed $value): void
public function offsetUnset(int $index): void
public function rewind(): void
public function __serialize(): array
public function setSize(int $size): true
public function toArray(): array
public function __unserialize(array $data): void
public function valid(): bool
#[\Deprecated(
    since: '8.4',
    message: 'this method is obsolete, as serialization hooks are provided by __unserialize() and __serialize()',
)]

public function __wakeup(): void
}

更新日志

版本 说明
8.4.0 Out of bounds accesses in SplFixedArray now throw exceptions of type OutOfBoundsException instead of RuntimeException. Because OutOfBoundsException is a child class of RuntimeException no behavioural changes are exhibited when attempting to catch those exceptions.
8.2.0 The SplFixedArray::__serialize() and SplFixedArray::__unserialize() magic methods have been added to SplFixedArray.
8.1.0 Using a non-integer key on a SplFixedArray now throws a TypeError instead of a RuntimeException.
8.1.0 SplFixedArray implements JsonSerializable now.
8.0.0 SplFixedArray implements IteratorAggregate now. Previously, Iterator was implemented instead.

示例

示例 #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 two blocks throw a RuntimeException: Index invalid or out of range
try {
    var_dump($array[-1]);
} catch(RuntimeException $e) {
    echo "RuntimeException: ".$e->getMessage()."\n";
}

try {
    var_dump($array[5]);
} catch(RuntimeException $e) {
    echo "RuntimeException: ".$e->getMessage()."\n";
}

// As of PHP 8.1.0, using a non-integer key throws a TypeError.
// Prior to PHP 8.1.0, a RuntimeException was thrown instead.
try {
    var_dump($array["non-numeric"]);
} catch(TypeError $e) {
    echo "TypeError: ".$e->getMessage()."\n";
}
?>

以上示例会输出:

NULL
int(2)
string(3) "foo"
RuntimeException: Index invalid or out of range
RuntimeException: Index invalid or out of range
TypeError: Illegal offset type

注意:

Prior to PHP 8.1.0, accessing a SplFixedArray with a non-integer key threw a RuntimeException instead of a TypeError.

目录

来源:PHP Documentation Group · 作者与贡献记录 · CC BY 3.0。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
PHP Documentation Group
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。