Php study notes array 1. generally, $ name ['Tom '] and $ name [tom] are the same. Keys without quotation marks cannot be different from constants, for example: when define ('index', 5); $ name ['Tom '] is different from $ name [tom]
2. it is a good habit to enclose variables in double quotes {}, such as echo "{$ name }";
3. array $ name = array ('Tom ', 'Jim', 'Alice '); and $ username = ['Tom', 'Jim ', 'Alice'];
4. the array uses the => symbol to specify an initial value. The subsequent key values are continuous;
Ps1: $ days = array (1 => "Mon", "Tue", "Wed", "Thu"); // The Tue key value is 2, the Wed key value is 3 and so on.
Ps2: $ days = array ('one' => "Mon", "Tue", "Wed", "Thu"); // The key value of array 2 is 0, the key value of array 3 is 1.
5. specified range: range ();
Ps1: $ num = range (2, 5); // $ num = array (2, 3, 4, 5 );
Ps2: $ str = range (a, z); // 26 letters of a-z are saved;
Ps3: $ num = range (5, 2); // $ num = array (5, 4, 3, 2 );
Ps4: $ str = range ("abd", "zds"); // the 26 letters of a-z are saved, only the first character range is limited, and the number is also
6. obtain the array size.
The count () and sizeof () functions have the same usage and effect;
For example, $ family = array ("tom", "jim", "cherry"); $ size = count ($ family); $ size is 3
7. fill in the array
Array_pad () uses repeated content to create an array. The first parameter is the array, and the second parameter is the minimum number of elements you want the array to have, the third parameter is the initial value of each element. a new array is returned and the source array is discarded.
Ps1:
$ Scores = array (5, 10 );
$ Padded = array_pad ($ scores, 5, 0); // $ padded is now array (5, 10, 0, 0, 0 );
Ps2:
$ Padded = array_pad ($ scores,-5, 0); // $ padded is now array (0, 0, 0, 5, 10 );
8. multi-dimensional array
To use variable replacement to access elements in multi-dimensional arrays, you must enclose the entire element in braces:
Echo "{$ num [1] [0]}"; // The result is 4.
Echo "$ num [1] [0]"; // The result is array [0].
9. retrieve multiple values
To copy all values in an array to a variable, you can use the list () structure;
$ Person = arrary ("tom", 35, "alice ");
List ($ name, $ age, $ wife) = $ person; // $ name is tom, $ age is 35, $ wift is alice
The list () function is usually used to extract data from a database query that returns only one record. in this way, the simple difference uxnde data is automatically assigned to a series of local variables .)
Ps: $ SQL = "select name, age, wife from person where id = 2 ";
$ Result = mysql_query ($ SQL );
List ($ name, $ age, $ wife) = mysql_fetch_assoc ($ result );
Ps2: if the number of elements in the array is equal to the number of variables in list (), extra values are ignored. if the number of elements in list () is greater than that in the array, extra values are set to NULL;
Use two consecutive commas (,) in list () to skip values in the array;
$ Values = range ('A', 'E ');
List ($ m, $ n, $ o) = $ values; // $ m is "a", $ n is "c", $ o is "e"