Example of converting php json to array-PHP source code

Source: Internet
Author: User
Tags php array to json php json php website
Php json to array is very convenient to implement. Because the json storage format has rules, php also comes with functions. We only need to use the corresponding functions to implement the function, as shown below. Php json-to-array conversion is very easy to implement. Because the json storage format has rules, php also comes with functions. We only need to use the corresponding functions to implement the conversion, the details are as follows.

Script ec (2); script

Convert arrays to Json in PHP

Powerful PHP provides built-in functions: json_encode () and json_decode (). It is easy to understand that json_encode () is to convert the PHP array to Json. On the contrary, json_decode () converts Json to a PHP array.

For example:

The Code is as follows:

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

Echo json_encode ($ array );

The program will print: {"name": "Eric", "age": 23}

Let's look at the following example:

The Code is as follows:

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

Echo json_encode ($ array );

The 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 an object-type Json. I already said. The two are called differently in js.
In fact, as long as there is a string key in the PHP array key, then json_encode () will return the Json in the object format. This is incorrect. Because, although PHP code does not produce errors, if such Json is passed to the JS function, JS regards this Json as an object, however, objects cannot use numbers as attribute names. That is to say, JS does not know what this is: user.0.username (in the middle is the number 0)

Example.

The Code is as follows:

$ Json_arr = array ('webname' => 'php website', 'website' => 'HTTP: // www.111cn.net ');
$ Php_json = json_encode ($ json_arr); // convert the php array format to json format data
Echo $ php_json;
$ Php_json = json_decode ($ php_json); // convert the data in json format to a php array.
Print_r ($ php_json );


Json Array

$ Json = '[{"id": "22", "name": "33", "descn": "44"}]'; // convert an array in json format to an array in php
$ Arr = (Array) json_decode ($ json );

Echo $ arr [0]-> id; // access using an object

The parameters after the json conversion data must contain true.

The Code is as follows:

$ A = < {
"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,
"Maid": 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 worth more than 5 points, and you have to buy other models. ",
"RateDate": "16:50:11 ",
"RateResult": 1,
"RatedUserNumId": 361988210,
"Reply ":"",
"Cmdinfo": null,
"Tag ":"",
"TradeClosingdate": null,
"Type": 0,
"UserVipLevel": 0
}
],
"ShowMore": 0
},
"BabyRateJsonList": "uri:/detailRate. vm cacheTime: 22:07:24 key: Invalid cacheArea: 531 aliveTime: 14400 cacheType: PAGE_CACHE_PAGE_TYPE ",
"DetailRate": "uri:/detailRate. vm cacheTime: 22:07:24 key: 9fb7a9a5d419da77109797ce1809367c 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.