A little conjecture about the array model in PHP (by Misko Lee)
according to my own understanding, the array should be a contiguous space in memory. It is necessary to open a blank memory in memory before the array is used, and if the declared size is less than the available contiguous memory size, a memory overflow error should be reported.
for children who have used a php Array, it should be able to quickly determine that the array in PHP does not have the above characteristics. One, the array declaration does not require a size determination ; second, the array does not use the upper limit. With these two points, we can tell that the array in PHP is a loosely structured form of a list, not a contiguous memory space.
Arrays in PHP can be divided into indexed arrays and associative arrays of two forms. An indexed array is a count-type array structure. And the associative array is the map data Structure An implementation, that is, the structure of Key-value.
We have some discussions on indexed arrays and associative arrays using the following example.
$arr =array ("the name ' = ' Misko_lee ', ' age ' =>22);// define an index, associative mixed-type array
For ($i =0; $i <> < span=""> <>
echo $arr [$i];// Here we can see an error indicating the subscript overflow
the Cout ($arr) function call returns The length of the $arr array. However , only the for loop can output an indexed array normally. Thus, we can judge, indexed arrays and associative arrays are two different implementations. It also proves that the array in PHP is a loosely-structured conjecture.
about indexed arrays We also have the following assumptions:
$arr [100]=100;
$arr []=101;// at this time $arr [] Auto-growth subscript is 101.
The above experiment proves our conjecture thatthe array counters in PHP are not counted from the first address of the array memory .It is a pseudo-counting method. Therefore, the use of counters to determine the size of the array is a complete error.
the above remarks are personal nonsense, with very unreliable factors. Welcome all Seniors Shoes pointed out the mistake. A good man is safe in life.
the last PS sentence: If the array uses a foreach traversal if it is not the reason for demand, use the reset () function to traverse the array pointer to reset