Php/docs/class.splfixedarray

来自菜鸟教程
跳转至:导航、​搜索

(PHP 5 >= 5.3.0, PHP 7)

简介

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 uses less memory than a standard 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

}

范例

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]); // NULLvar_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 rangetry {    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

Table of Contents