PHP Array and JSON learning

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.