1. array definition:
$ Arr [0] = 1;
$ Arr [1] = "hello ";
$ Arr [3] = 2.5;
[0]: indicates the subscript of an array, or a keyword.
$ Arr [0]: represents an array element.
$ Arr [0] = represents the array value
$ Arr: the name of the array.
Eg:
<? Php
// Define an array
$ Arr [0] = 1;
$ Arr [1] = "hello ";
$ Arr [2] = 2.5;
$ Arr [3] = true;
$ Arr [4] = null; // a space is required even if an empty value is entered.
Echo 'total'. count ($ arr). 'array elements <br/>'; // the system function count calculates the number of elements.
// Traverse
For ($ I = 0; $ I <count ($ arr); $ I ++ ){
Echo 'quarter '. ($ I + 1). "Is". $ arr [$ I].' <br/> ';
}
?>
Eg2:
$ Arr ["logo"] = "Beijing ";
$ Arr ["pwm"] = 123;
$ Arr [123] = "abc ";
// Traverse
/*
For ($ I = 0; $ I <count ($ arr); $ I ++ ){
Echo $ arr [$ I];
}*/
// Foreach traversal applies to a wider range
Foreach ($ arr as $ key => $ val ){
Echo $ key. '='. $ val. '<br/> ';
}
Result:
Logo = Beijing
Pwm = 1, 123
123 = abc
Note:
1. // *********** special instructions
$ Arr = array (5 => "logo",); // if the following table is not defined for the array, PHP automatically uses the largest number in front as the first subscript, the following element subscript automatically adds 1
// You can write the value of the logo array in this way.
// $ Arr [5] = "yes"; the element value is replaced.
Echo $ arr [5]. '<br/> ';
Echo $ arr [7];
2. // use decimals as the key. The PHP system automatically truncates the fractional part.
$ A = array ('abc' => 12, 'ppp '=> 234, 12.7 => "world ");
Print_r ($ );
Result: Array ([abc] => 12 [ppp] => 234 [12] => world)
3. Use true false null as the keyword (subscript)
// Use true false null as the keyword (subscript)
$ A [true] = "what"; // equivalent to $ a [1]
$ A [false] = "hahaa"; // equivalent to $ a [0]
$ A [null] = "China"; // equivalent to $ a [null] or $ a [""]
// Such output
Echo $ a [0]. '<br/> ';
Echo $ a [null]. '<br/> ';
$ A [22.6] = 133; // use a decimal number as the key. The PHP system automatically truncates the decimal part.
// Use print_r to output an array to display the array.
Print_r ($ );
Echo '<br/> ';
// Use var_dump to display the array information in more detail.
Var_dump ($ );
Effect:
Hahaa
China
Array ([1] => what [0] => hahaa [] = & gt; China [22] = & gt; 133
)
Array (4) {[1] => string (4) "what" [0] => string (5) "hahaa" [""] =>
String (6) "China" [22] => int (133 )}
4. PHP array can automatically grow
// PHP array can automatically increase dynamically
$ A = array (3, 4 );
$ A [3] = 4;
// Echo $ a [2]; // If the referenced array (array element) does not exist, the system reports an error.
Print_r ($ );
5. Several important functions in the array
$ A = array (1, 2, 3, "one", "two", "three ");
Echo count ($ a). '<br/>'; // count the number of elements in the function.
Echo is_array ($ a). '<br/>'; // determines whether it is an array.
Print_r ($ );
Echo '<br/> ';
Var_dump ($ a); // display array information
$ B = "1 2 3 'one' two' 'three '";
Echo '<br/> -------------------------';
Print_r (explode ("", $ B ));
$ B = array ();
Echo count ($ B );