Home >Backend Development >PHP Tutorial >PHP array classes and objects interface usage (1/3)_PHP tutorial
1. Array
The array in the PHP tutorial is actually an associative array, or a hash table. PHP does not need to declare the size of the array in advance, and can create the array by direct assignment. For example:
//The most traditional, use numbers as keys and assign values
$state[0]="beijing";
$state[1]="hebei" ;
$state[2]="tianjin";
//If the key is an increasing number, you can omit it
$city[]="shanghai";
$city[]="tianjin";
$city[]="guangzhou";
//Use string as key
$capital["china"] ="beijing";
$capital["japan"]="tokyo";
It will be more convenient to use array() to create an array. You can pass the array elements to it as array parameters. You can also create associative arrays using the => operator. For example:
$p=array(1,3,5,7);
$capital=array(“china”=>”beijing”, “japan=> ;"tokyo");
array is actually a grammatical structure, not a function. Similar to array, there is also a list(), which can be used to extract the values in the array and give Multiple variable assignments. For example:
list($s,$t)=$city;
echo $s,' ',$t;
Output result: shanghai tianjin
Note that the list method can only be used for arrays indexed by numbers.
PHP has built-in some common array processing functions. For details, please refer to the manual. Examples of commonly used functions are as follows, count or sizeof. You can get the length of the array, array_merge can merge two or more arrays, and array_push(pop) can use arrays like a stack.
1 2 3