SplFixedArray主要是處理數組相關的主要功能,與普通php array不同的是,**它是固定長度的,且以數字為鍵名的數組**,優勢就是比普通的數組處理更快。
## 類摘要
```php
SplFixedArray implements Iterator , ArrayAccess , Countable {
/* 方法 */
public __construct ([ int $size = 0 ] )
public int count ( void )
public mixed current ( void )
//↓↓導入PHP數組,返回SplFixedArray對象;
public static SplFixedArray fromArray ( array $array [, bool $save_indexes = true ] )
//↓↓把SplFixedArray對象數組導出為真正的數組;
public array toArray ( void )
public int getSize ( void )
public int key ( void )
public void next ( void )
public bool offsetExists ( int $index )
public mixed offsetGet ( int $index )
public void offsetSet ( int $index , mixed $newval )
public void offsetUnset ( int $index )
public void rewind ( void )
public int setSize ( int $size )
public bool valid ( void )
public void __wakeup ( void )
}
```
## Example
```php
# Example1:
$arr = new SplFixedArray(4);
try{
$arr[0] = 'php';
$arr[1] = 'Java';
$arr[3] = 'javascript';
$arr[5] = 'mysql';
}catch(RuntimeException $e){
//由于是定長數組,所以$arr超過定長4;就會拋出異常。
echo $e->getMessage(),' : ',$e->getCode();
}
#Example2:
// public static SplFixedArray fromArray ( array $array [, bool $save_indexes = true ] )
$arr = [
'4' => 'php',
'5' => 'javascript',
'0' => 'node.js',
'2' => 'linux'
];
//第二參數默認為true的話,保持原索引,如果為false的話,就重組索引;
//如下,如果重組了索引,那數組長度就為4;如果不重組長度就為6;
$arrObj = SplFixedArray::fromArray($arr);
$arrObj->rewind();
while($arrObj->valid()){
echo $arrObj->key(),'=>',$arrObj->current();
echo PHP_EOL;
$arrObj->next();
}
//↓↓由定長數組對象轉換為真正的數組
$arr = $arrObj->toArray();
print_r($arr);
```
- 現代化PHP特性
- php7常用特性整理
- 反射機制Reflection
- 依賴注入與服務容器
- 抽象類與接口
- 類多繼承的替代方案Traits
- 類的延遲綁定(后期綁定)
- 生成器語法
- 匿名函數和閉包
- 匿名類
- 理解php的output buffer
- 斷言ASSERT
- 魔術方法小結
- Zend Opcache字節碼緩存
- 內置的http服務器
- SPL標準庫
- 【SPL標準庫專題(1)】SPL簡介
- 【SPL標準庫專題(2)】Iterator
- 【SPL標準庫專題(3)】Classes
- 【SPL標準庫專題(4)】Exceptions
- 【SPL標準庫專題(5)】Datastructures:SplDoublyLinkedList
- 【SPL標準庫專題(6)】Datastructures:SplStack & SplQueue
- 【SPL標準庫專題(7)】Datastructures:SplPriorityQueue
- 【SPL標準庫專題(8)】Datastructures:SplHeap & SplMaxHeap & SplMinHeap
- 【SPL標準庫專題(9)】Datastructures:SplFixedArray
- 【SPL標準庫專題(10)】Datastructures:SplObjectStorage
- PHPcomposer使用手札[ing]
- PHP中的多態
- 通過命名空間實現自動加載的框架雛形
- 日期與金額
- PHPstorm使用攻略
- 筆記本