Data structure - array (array)
Arrays are structures that store data in a contiguous manner and are accessible through indexing. Don't confuse them with PHP arrays: PHP arrays are actually implemented as ordered hash tables.
Key differences between SplFixedArray and PHP arrays:
SplFixedArray is a fixed-length standard (standard array) and only allows integers within the range as indexes. The advantage is that it allows faster array implementation.
PHP arrays are actually implemented as ordered hash tables (a collection of data).
/*
Construct a new fixed array with a specified length of 5
*/
$array = new SplFixedArray(5);
/*
Assign a value to the specified index
*/
$array[1] = 2;
$array[4] = "foo";
/*
Data structure:
object(SplFixedArray)#1 (5) {
[0]=>
NULL
[1]=>
int(2)
[2]=>
NULL
[3]=>
NULL
[4]=>
string(3) "foo"
}
*/
var_dump($array);
/*
Array length is 5
*/
var_dump($array->count());
/*
Increase the size of the array to 10
*/
$array->setSize(10);
/*
As the length of the array increases, the original data will not change
object(SplFixedArray)#1 (10) {
[0]=>
NULL
[1]=>
int(2)
[2]=>
NULL
[3]=>
NULL
[4]=>
string(3) "foo"
[5]=>
NULL
[6]=>
NULL
[7]=>
NULL
[8]=>
NULL
[9]=>
NULL
}
*/
var_dump($array);
/*
Assign a value to the extended array
*/
$array[9] = "asdf";
/*
Reduce the array to size 2
Will save two lengths from the beginning of the index
object(SplFixedArray)#1 (2) {
[0]=>
NULL
[1]=>
int(2)
}
*/
$array->setSize(2);
/*
The following line throws a RuntimeException: Index is invalid or out of range
*/
try {
$array["username"]="jack";
} catch(RuntimeException $re) {
/*
RuntimeException: Index invalid or out of range
Indexes can only be integers
*/
echo "RuntimeException: ".$re->getMessage()."n";
}
try {
$array[-1]="jack";
} catch(RuntimeException $re) {
/*
RuntimeException: Index invalid or out of range
The index is invalid
*/
echo "RuntimeException: ".$re->getMessage()."n";
}
try {
$array[5]="jack";
} catch(RuntimeException $re) {
/*
RuntimeException: Index invalid or out of range
Index exceeds array length
*/
echo "RuntimeException: ".$re->getMessage()."n";
}
/*
View array size
getSize/count
*/
echo $array->getSize();
?>
$data=[1 => 1, 0 => 2, 3 => 3];
$sfa = SplFixedArray::fromArray($data);
/*
object(SplFixedArray)#1 (4) {
[0]=>
int(2)
[1]=>
int(1)
[2]=>
NULL
[3]=>
int(3)
}
*/
var_dump($sfa);
$data=[1 => 1, 2 => 2, true => 3,5=>5];
$sfa = SplFixedArray::fromArray($data);
/*
1 true is converted to 1, and the data with index 1 will be changed to 3
2 The original index is saved by default, and the index starts from 0. If the data does not exist, the default is NULL
object(SplFixedArray)#2 (6) {
[0]=>
NULL
[1]=>
int(3)
[2]=>
int(2)
[3]=>
NULL
[4]=>
NULL
[5]=>
int(5)
}
*/
var_dump($sfa);
$data=[1 => 1, 2 => 2, true => 3,5=>5];
$sfa = SplFixedArray::fromArray($data,false);
/*
1 Try to save the numeric index used in the original array. Default is true;
2 If the original numerical index is not saved, a non-NULL array will be returned
object(SplFixedArray)#1 (3) {
[0]=>
int(3)
[1]=>
int(2)
[2]=>
int(5)
}
*/
var_dump($sfa);
$data=[1 => 1, 'a' => 2, true => 3];
/*
Index must be an integer
Fatal error: Uncaught exception 'InvalidArgumentException' with message 'array must contain only positive integer keys'
*/
//$sfa = SplFixedArray::fromArray($data);
?>