Measure the test taker's knowledge about Array, foreach, and arrayforeach in PHP.
1. Understand Arrays
Arrays in PHP are actually an ordered ing. Ing isValuesAssociatedKeys. For more information, see Array in PHP.net.
2. Example: A general Array
Here, I use a simple example and use the graphical method to understand arrays.
//1.-----------------------------------$a = array(3 => 'a', 1 => 'b', 2 => 'c');echo var_dump($a);
[Note]: use arrows to describe the data values of each unit in array $ a corresponding to a memory address (in fact, its internal structure adopts the HashTable structure, refer to the Hash algorithm in PHP written by laruence ).
3. Example: Add a reference to the array definition.
//2.-----------------------------------$x = 'x';$a = array(3 => 'a', 1 => &$x, 2 => 'c');echo "
$ A [1] and $ x correspond to the same data. When var_dump ($ a) is used, multiple & symbols (& string (1) "x") of the array's 2nd units are displayed, indicating reference.
When you change the value of $ x to 'y', it is equivalent to modifying the value of $ a [1] to 'y '.
You can clearly describe this change:
4. Example: Use foreach to traverse the array.
//3.-----------------------------------$a = array(3 => 'a', 1 => 'b', 2 => 'c');echo "
In each loop, the unit value in the current array is assigned to $ value.KeyThe key is assigned to $ key. As described in:
[Note:] The gray virtual arrow indicates giving a value.
5. Example: Use the reference value assignment in the foreach traversal array.
//4.-----------------------------------$a = array(3 => 'a', 1 => 'b', 2 => 'c');echo "
In each loop, $ value points to the value of the unit in the current array, and then executes the code "$ value. = 'n';", as described in:
6. Example: further analysis of Example 5.
In Example 5, after foreach traverses the array, the $ value variable is not automatically destroyed, pointing to the same data as the last unit $ a [2] of array $.
At this time, the value of $ value is changed, that is, the value of $ a [2] is changed.
//5.-----------------------------------$value='m';echo "
Instance verification, the last element of the array$ ValueReference inForeachIt is retained after the loop. We recommend that you use unset () to destroy it.
7. Summary
The above example only describes some features of array and foreach in php. Finally, I feel that the array and foreach in php are different from other programming languages. PHP cannot be analyzed using a structure similar to the C language.
(End .)