PHP JSON format Control detailed

Source: Internet
Author: User
Tags php json
JSON is widely used in data transmission for its high-performance transmission and resolution. This paper mainly introduces the detailed analysis of PHP JSON format control related data. We hope to help you.

On the question of JSON, there are new friends to ask me, such as why I output is {"1": "Item1", "2": "Item2", "3": "Item3"} instead of ["Item1", "item2", "Item3"].

PHP Arrays and JS arrays

I use PHP 5.4 above syntax.

PHP is associated with arrays and indexed arrays, for example:


<?php//indexed array $arr = [' item1 ', ' item2 ', ' item3 '];//associative array $arr = [' name ' = ' Zhang San ', ' age ' = ' 22 ',];


And JS there is only one array, that is the index array, you might say you can use the K/V key-value pair form to simulate associative arrays ah.

The K/V key value pair looks like, but he doesn't have any array features, which is not explained in detail here.

The JSON format obtained by the above PHP array json_encode is ["Item1", "item2", "Item3"] and {"Name": "\u5f20\u4e09", "Age": "22"}. Here the Chinese is converted to Unicode, if you want to display Chinese, PHP 5.4 After support json_unescaped_unicode parameters, Json_encode ($arr, Json_unescaped_unicode) can get {" Name ":" Zhang San "," Age ":" 22 "}, but it is highly deprecated.

Here is the JS array and the object format of the JSON string, then why the two types are generated, or, what will generate the object format, what will generate array format?

PHP Array output Format control

I've come up with a list of things, just look at the code.


<?php$arr = [//Not 0 start, will output object 1 = ' item1 ', 2 = ' item2 ', 3 = ' item3 ',];echo "output object:", Json_encode ($arr), "\ n ";//Output object: {" 1 ":" Item1 "," 2 ":" Item2 "," 3 ":" Item3 "} $arr = [///continuous index, output array 0 = ' item1 ', 1 = ' item2 ', 2 = ' item3 ',] echo "Output array:", Json_encode ($arr), "\ n";//output array: ["Item1", "item2", "item3"] $arr = [//continuous index, output array ' item1 ', ' item2 ', ' item3 ' ,];echo "Output array:", Json_encode ($arr), "\ n";//output array: ["Item1", "item2", "item3"] $arr = [//index discontinuous, output object 0 = ' item1 ', 1 =&gt ; ' item2 ', 2 = ' Item3 ', 5 = ' item5 ',];echo "output object:", Json_encode ($arr), "\ n";//Output object: {"0": "Item1", "1": "Item2", "2": "  Item3 "," 5 ":" ITEM5 "} $arr = [//contains associated index, certain output object 0 = ' item1 ', 1 = ' item2 ', 2 = ' item3 ', ' other ' + ' other fields '];echo "Output object:", Json_encode ($arr), "\ n";//Output object: {"0": "Item1", "1": "Item2", "2": "Item3", "Other": "\u5176\u4ed6\u5b57\u6bb5"} Associative array + indexed array instance $arr = [//associative array ' other ' + ' other fields ', ' count ' = = 3,//array of ' list ' + = [//index array ' item1 ', ' item2 ' , ' Item3 ',],];echo ' object + array: ", Json_encode ($arr)," \ n ";//Object + array: {" Other ":" \u5176\u4ed6\u5b57\u6bb5 "," Count ": 3," list ": [" Item1 "," item2 "," Item3 " ]}


In fact, the first is a lot of novice friends often encounter problems.

Since the database is read out and they like to cite the ID as a reference, and the database ID is not starting from 0, see this example.


$arr = $User->where ($where)->find (); Read Data $list = [];foreach ($arr as $key + $val) {//traversal array $list [$key] = [  ' name ' = + $val [' name '],  ' age ' => ; $val [' age '],];} $list [' count '] = count ($arr); Other properties Echo Json_encode ($list); Output json//{"1": {"name": "Zhangsan", "Age": $}, "2": {"name": "Lisi", "Age":}, "Count": 2}


The last one is a more commonly used notation, with custom fields and arrays used to modify the example.


$arr = $User->where ($where)->find (); Read Data $list = [];foreach ($arr as $key + $val) {//traversal array $list [' list '] [] = [//Modify here  ' name ' = + $val [' name '],
   ' age ' = $val [' Age '], ';} $list [' count '] = count ($arr); Other properties Echo Json_encode ($list); Output json//{"List": [{"Name": "Zhangsan", "Age": 22},{"name": "Lisi", "Age": +}], "Count": 2}



Related recommendations:

PHP solve JSON Chinese display problem

PHP convert indexed array to JSON

Examples of using Json_encode in PHP

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.