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). <?php<br />
<br />
/*<br />
Construct a new fixed array with a specified length of 5<br />
*/<br />
$array = new SplFixedArray(5);<br />
<br />
/*<br />
Assign a value to the specified index<br />
*/<br />
$array[1] = 2;<br />
$array[4] = "foo";<br />
<br />
/*<br />
Data structure: <br />
object(SplFixedArray)#1 (5) {<br />
[0]=><br>
NULL<br>
[1]=><br>
int(2)<br>
[2]=><br>
NULL<br>
[3]=><br>
NULL<br>
[4]=><br>
string(3) "foo"<br>
}<br>
*/<br>
var_dump($array);<br>
<br>
/*<br>
Array length is 5<br>
*/<br>
var_dump($array->count());<br>
<br>
/*<br>
Increase the size of the array to 10<br>
*/<br>
$array->setSize(10);<br>
<br>
/*<br>
As the length of the array increases, the original data will not change<br>
object(SplFixedArray)#1 (10) {<br>
[0]=><br>
NULL<br>
[1]=><br>
int(2)<br>
[2]=><br>
NULL<br>
[3]=><br>
NULL<br>
[4]=><br>
string(3) "foo"<br>
[5]=><br>
NULL<br>
[6]=><br>
NULL<br>
[7]=><br>
NULL<br>
[8]=><br>
NULL<br>
[9]=><br>
NULL<br>
}<br>
<br>
*/<br>
var_dump($array);<br>
<br>
/*<br>
Assign a value to the extended array<br>
*/<br>
$array[9] = "asdf";<br>
<br>
<br>
/*<br>
Reduce the array to size 2<br>
Will save two lengths from the beginning of the index <br>
object(SplFixedArray)#1 (2) {<br>
[0]=><br>
NULL<br>
[1]=><br>
int(2)<br>
}<br>
*/<br>
$array->setSize(2);<br>
<br>
<br>
<br>
/*<br>
The following line throws a RuntimeException: Index is invalid or out of range <br>
*/ <br>
try {<br>
$array["username"]="jack";<br>
} catch(RuntimeException $re) {<br>
/*<br>
RuntimeException: Index invalid or out of range<br>
Indexes can only be integers<br>
*/<br>
echo "RuntimeException: ".$re->getMessage()."n"; <br>
}<br>
<br>
try {<br>
$array[-1]="jack";<br>
} catch(RuntimeException $re) {<br>
/*<br>
RuntimeException: Index invalid or out of range<br>
The index is invalid<br>
*/<br>
echo "RuntimeException: ".$re->getMessage()."n";<br>
}<br>
<br>
try {<br>
$array[5]="jack";<br>
} catch(RuntimeException $re) {<br>
/*<br>
RuntimeException: Index invalid or out of range<br>
Index exceeds array length<br>
*/<br>
echo "RuntimeException: ".$re->getMessage()."n";<br>
}<br>
<br>
/*<br>
View array size<br>
getSize/count <br>
*/<br>
echo $array->getSize();<br>
?>
<?php<br />
<br />
$data=[1 => 1, 0 => 2, 3 => 3];<br>
<br>
$sfa = SplFixedArray::fromArray($data);<br>
<br>
/*<br>
object(SplFixedArray)#1 (4) {<br>
[0]=><br>
int(2)<br>
[1]=><br>
int(1)<br>
[2]=><br>
NULL<br>
[3]=><br>
int(3)<br>
}<br>
*/<br>
var_dump($sfa);<br>
<br>
$data=[1 => 1, 2 => 2, true => 3,5=>5];<br>$sfa = SplFixedArray::fromArray($data);<br>
<br>
/*<br>
1 true is converted to 1, and the data with index 1 will be changed to 3<br>
2 The original index is saved by default, and the index starts from 0. If the data does not exist, the default is NULL<br>
object(SplFixedArray)#2 (6) {<br>
[0]=><br>
NULL<br>
[1]=><br>
int(3)<br>
[2]=><br>
int(2)<br>
[3]=><br>
NULL<br>
[4]=><br>
NULL<br>
[5]=><br>
int(5)<br>
}<br>
*/<br>
var_dump($sfa);<br>
<br>
$data=[1 => 1, 2 => 2, true => 3,5=>5];<br>
$sfa = SplFixedArray::fromArray($data,false);<br>
<br>
/*<br>
1 Try to save the numeric index used in the original array. Default is true; <br>
2 If the original numerical index is not saved, a non-NULL array will be returned<br>
<br>
object(SplFixedArray)#1 (3) {<br>
[0]=><br>
int(3)<br>
[1]=><br>
int(2)<br>
[2]=><br>
int(5)<br>
}<br>
<br>
*/<br>
var_dump($sfa);<br>
<br>
<br>
$data=[1 => 1, 'a' => 2, true => 3];<br>
/*<br>
Index must be an integer <br>
Fatal error: Uncaught exception 'InvalidArgumentException' with message 'array must contain only positive integer keys'<br>
*/<br>
//$sfa = SplFixedArray::fromArray($data);<br>
<br>
?>