首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

SplFixedArray (class)

Introduction

(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 allows a faster array implementation.

Class synopsis

SplFixedArray implements Iterator , ArrayAccess , Countable {

/* Methods */

public __construct ( int $size = 0 )

代码语言:javascript
复制
public int count ( void )
代码语言:javascript
复制
public mixed current ( void )
代码语言:javascript
复制
public static SplFixedArray fromArray ( array $array [, bool $save_indexes = true ] )
代码语言:javascript
复制
public int getSize ( void )
代码语言:javascript
复制
public int key ( void )
代码语言:javascript
复制
public void next ( void )
代码语言:javascript
复制
public bool offsetExists ( int $index )
代码语言:javascript
复制
public mixed offsetGet ( int $index )
代码语言:javascript
复制
public void offsetSet ( int $index , mixed $newval )
代码语言:javascript
复制
public void offsetUnset ( int $index )
代码语言:javascript
复制
public void rewind ( void )
代码语言:javascript
复制
public bool setSize ( int $size )
代码语言:javascript
复制
public array toArray ( void )
代码语言:javascript
复制
public bool valid ( void )
代码语言:javascript
复制
public void __wakeup ( void )

}

Examples

Example #1 SplFixedArray usage example

代码语言:javascript
复制
<?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:

代码语言:javascript
复制
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::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

← SplPriorityQueue::valid

SplFixedArray::__construct →

代码语言:txt
复制
 © 1997–2017 The PHP Documentation Group

Licensed under the Creative Commons Attribution License v3.0 or later.

扫码关注腾讯云开发者

领取腾讯云代金券