(1), the traversal order of the PHP array
Give me a chestnut first:
<?php$arr[' a '] = ' 123 '; $arr [' b '] = ' 456 '; $arr [' c '] = ' 789 '; foreach ($a as $val) {var_dump ($val);}
The output of this code will obviously get the following result:
So, what if it's the following code?
<?PHP$ARR[2] = ' 123 '; $arr [1] = ' 456 '; $arr [0] = ' 789 '; foreach ($a as $val) {var_dump ($val);}
The difference between these two pieces of code is that an array is an associative array, and the other array is an indexed array, then their output will be the same, the answer is the same, the second code output will be the same as the first paragraph of the result.
So, what is this for?
The reason is that the implementation of the PHP array is a large hashtable, adding elements, the elements will be inserted in the same hash element chain of the head and the tail of the linear list. That is, the elements are traversed in a linear traversal according to the order in which they are inserted, and this particular design makes it possible in PHP that when a numeric index is used, the order of the elements is determined by the sequence of additions, not the index order. In other words, the order in which the arrays are traversed in PHP is related to the addition of elements.
For a detailed explanation, please refer to Brother Bird's in-depth understanding of PHP arrays (traversal order)
(2), what kind of two associative arrays are strictly equal
Let's look at the following code:
<?php$a = Array (' 1 ' = ' a ', ' 2 ' = ' B '); $b = Array (' 2 ' = ' B ', ' 1 ' = ' a '); $c = Array (' 1 ' = ' a ', ' 2 ' = ' B '); $flag _a_equal_b = ($a = = $b) $flag _a_strict_equal_b = ($a = = = $b); $flag _a_strict_equal_c = ($a = = = = $c); Var_dump ($fla G_a_equal_b); Var_dump ($flag _a_strict_equal_b); Var_dump ($flag _a_strict_equal_c);
Running this code will result in the following:
That is, in PHP, as long as all the keys of two associative arrays, the corresponding value is consistent, then the two associative arrays are equal. However, these two associative arrays are strictly equal only if all keys of the two associative arrays are in the same order.
What you should know about PHP arrays