Php/docs/class.splfixedarray

提供:Dev Guides
< Php
移動先:案内検索

(PHP 5 >= 5.3.0, PHP 7)

はじめに

SplFixedArray クラスは配列の主要な機能を提供します。 SplFixedArray と通常の PHP の配列との主な違いは、 SplFixedArray は固定長であって、 整数値で指定した範囲内の添字しか使用できないところです。 これにより、標準の array よりメモリ消費が少なくて済みます。


クラス概要


SplFixedArray implements Iterator , ArrayAccess , Countable {

/* メソッド */

public __construct ([ int $size = 0 ] )

public count ( ) : int

public current ( ) : mixed

public static fromArray ( array $array [, bool $save_indexes = true ] ) : SplFixedArray

public getSize ( ) : int

public key ( ) : int

public next ( ) : void

public offsetExists ( int $index ) : bool

public offsetGet ( int $index ) : mixed

public offsetSet ( int $index , mixed $newval ) : void

public offsetUnset ( int $index ) : void

public rewind ( ) : void

public setSize ( int $size ) : bool

public toArray ( ) : array

public valid ( ) : bool

public __wakeup ( ) : void

}

例1 SplFixedArray の使用例

<?php// 固定長の配列を初期化します$array = new SplFixedArray(5);$array[1] = 2;$array[4] = "foo";var_dump($array[0]); // NULLvar_dump($array[1]); // int(2)var_dump($array["4"]); // string(3) "foo"// 配列のサイズを 10 に拡大します$array->setSize(10);$array[9] = "asdf";// 配列のサイズを 2 に縮めます$array->setSize(2);// 以下は 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";}?>

上の例の出力は以下となります。


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

目次