1. Array
The array in the php tutorial is actually an associated array, or a hash table. Php does not need to declare the array size in advance. You can create an array by directly assigning values. For example:
// The most traditional method: use numbers as keys and assign values.
$ State [0] = "beijing ";
$ State [1] = "hebei ";
$ State [2] = "tianjin ";
// If the key is an incremental number, it can be omitted
$ City [] = "shanghai ";
$ City [] = "tianjin ";
$ City [] = "guangzhou ";
// Use a string as the key
$ Capital ["china"] = "beijing ";
$ Capital ["japan"] = "tokyo ";
It is more convenient to use array () to create an array. You can use an array element as an array parameter and use the => operator to create an associated array. For example:
$ P = array (1, 3, 5, 7 );
$ Capital = array ("china" => "beijing", "japan =>" tokyo ");
Array is actually a syntax structure, not a function. Similar to array, there is also a list (), which can be used to extract values from the array and assign values to multiple variables. For example:
List ($ s, $ t) = $ city;
Echo $ s, '', $ t;
Output result: shanghai tianjin
Note: The list method can only be used for arrays indexed by numbers.
Php has built-in some common array processing functions. For details, refer to the manual. Examples of common functions are as follows. count or sizeof can get the length of the array. array_merge can combine two or more arrays. array_push (pop) can use arrays like stacks.
1 2 3