Example of a PHP json-to-turn array

Source: Internet
Author: User
Tags php code php json php web development

Converting arrays into JSON in PHP

Powerful PHP already provides built-in functions: Json_encode () and Json_decode (). It's easy to understand that Json_encode () is converting a PHP array into JSON. Instead, Json_decode () converts JSON into a PHP array.

For example:

The code is as follows Copy Code

$array = Array ("name" => "Eric", "age" => 23);

echo Json_encode ($array);

Program will print out: {"name": "Eric", "Age": 23}

Look at the following example:

The code is as follows Copy Code

$array = Array (0 => "Eric", 1 => 23);

echo Json_encode ($array);

Program will print out: ["Eric", 23]

The above two examples show that if the keys of the PHP array are numbers, then Json_encode () returns an array of JSON, if the keys of the PHP array are all strings. Then Json_encode () returns JSON in the form of an object. Just already said. The two in the JS call is different.
In fact, as long as there is a string form key in the key of the PHP array, Json_encode () returns the JSON of the object form. This is not true. Because, although there is no error in the PHP code, but if you pass this JSON to the JS function, JS will use this JSON as an object, and the object is not possible to use the number as the attribute name. That is, JS does not know what this is: User.0.username (the middle is the number 0)

Example.

The code is as follows Copy Code

$json _arr = Array (' WebName ' => ' PHP Web Development Tutorial Network ', ' WebSite ' => ' http://www.111cn.net ');
$php _json = Json_encode ($json _arr); Converts the PHP array format into JSON-formatted data
echo $php _json;
$php _json = Json_decode ($php _json); Then convert the JSON-formatted data into a PHP array
Print_r ($php _json);


JSON array

$json = ' [{"id]:", "name": "DESCN": "44"}] '; JSON-formatted array into an array of PHP
$arr = (Array) json_decode ($json);

Echo $arr [0]->id; Accessing in the form of objects

JSON transform data after the parameter to take ture

The code is as follows Copy Code

$a = <<<eod
{
"Scoreinfo": {
"Isb2cseller": true,
"Nomark": false,
"Merchandisscore": "5.0",
"Merchandistotal": 1,
"width": 100
},
"Ratelistinfo": {
"Watershed": 100,
"Paginator": {
"Beginindex": 1,
"Endindex": 1,
"FirstPage": 1,
"Items": 1,
"ItemsPerPage": 8,
"LastPage": 1,
"Length": 1,
"Offset": 0,
"Page": 1,
"Pages": 1
},
"Ratelist": [
{
"Alimallseller": false,
"Anony": true,
"Auctionsku": "Color classification: Blue + black; Size: 36",
"Award": "",
"B2cseller": false,
"DispalyRateLevel1": 0,
"DispalyRateLevel2": 0,
"Dispalyratesum": 0,
"Displayratepic": "",
"Displayuserlink": "Http://i.taobao.com/u/MTExNTE1Mjk5MQ==/tlive/taodan_list.htm?from=rate",
"Displayusernick": "L***1",
"Displayusernumid": 0,
"Displayuserratelink": "Http://rate.taobao.com/rate.htm?user_id=0&rater=1",
"Displayuserseller": false,
"Fee Dappendeddo": null,
"Fromb2cmapping": false,
"Frommall": false,
"Fromonepai": false,
"Fromvirtualhurling": false,
"Gapdates": 0,
"id": 63810867055,
"Ratecontent": "The seller's service attitude is really good, your shoes are more value, 5 points, but also in the purchase of other money." ",
"Ratedate": "2013.04.14 16:50:11",
"Rateresult": 1,
"Ratedusernumid": 361988210,
"Reply": "",
"Shareinfo": null,
"Tag": "",
"Tradeclosingdate": null,
' type ': 0,
"Userviplevel": 0
}
],
"ShowMore": 0
},
"Babyratejsonlist": "uri:/DETAILRATE.VM cachetime:2013-04-26 22:07:24 key:9fb7a9a5d419da77465797ce1809367c cachearea:531 alivetime:14400 Cachetype:page_cache_page_type ",
"Detailrate": "uri:/DETAILRATE.VM cachetime:2013-04-26 22:07:24 key:9fb7a9a5d419da77465797ce1809367c cachearea:531 alivetime:14400 Cachetype:page_cache_page_type "
}
EOD;
Var_dump (Json_decode ($a, 1));

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.