in PHP, there are three types of arrays :
- Indexed array-an array with a numeric index (you can understand the array in the C/c++/java, accessed directly by the index location)
- Associative array-An array with the specified key (which can be understood as a map, stored by key value, accessed by key)
- Multidimensional arrays-An array that contains one or more arrays, which can be understood as complex arrays, where elements can be ordinary elements or arrays.
1. Indexed arrays
An array that can be accessed using the index (subscript) of the data. As in the following code:
1<?PHP2 $myArray=Array();3 $myArray[]=4;//add an element to the end, equivalent to Array_push ()4 $myArray[]=5;//add an element to the end, equivalent to Array_push ()5 Echo $myArray[0]. " \ n ";//16 Echo $myArray[2]. " \ n ";//47?>
2. Associative arrays
Saves key value pairs of data, and data is accessed by key name. If a key with the same name appears, then add the previous data. As in the following code:
1<?PHP2 $myArray=Array("Name" = "Liu");3 $myArray["Name"]= "Wang";4 $myArray["School"]= "Bupt";5 foreach($myArray as $key=$value){6 Echo $key.":".$value." \ n ";7 }8 /*9 Output:Ten Name:wang One School:bupt A */ -?>
3. Multidimensional arrays
The elements of an array can be ordinary elements or other elements. As in the following code:
1 <? PHP 2 $indexArray=array(+/-); 3 $myArray=array($indexArray, "6" = "Liu"); 4 $myArray ["Age"]=18; 5 $myArray []= "Bupt"; 6 Print_r ($myArray); 7 ?>
Output:
Conversion of 4.PHP arrays to JSON format
PHP provides functions that can be converted directly:
(1) Json_encode : PHP Array---> JSON string
(2) Json_decode : JSON string---> PHP array (you need to set the second parameter to TRUE)
PHP Array and JSON learning