In-depth analysis of PHP JSON format control,
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:
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 => ; ' 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) {//Iterate over array $list [$key] = [ ' name ' + = $val [' name '],
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) {//Iterate through the array $list [' list '] [] = [//Modify here ' name ' = + $val [' name '],
I hope that you learn PHP JSON format control related knowledge has helped. Thank you for all the support you have been giving to our website.
Articles you may be interested in:
- PHP functions for GBK page output in JSON format
- PHP Infinite-level data JSON format and JS parsing
- PHP JSON Format Data Interaction Instance code detailed
- Implementation code for PHP operation JSON format data
- Parsing PHP using Curl to submit JSON-formatted data
- PHP uses Curl to send JSON-formatted data instances
- How PHP determines whether it is a JSON format
- Methods for defining literal objects in PHP using JSON data format
- How to convert PHP arrays into JSON format
http://www.bkjia.com/PHPjc/1084550.html www.bkjia.com true http://www.bkjia.com/PHPjc/1084550.html techarticle In- depth analysis of the PHP JSON format control, on the question of JSON, a few new friends to ask me, such as why I output is {"1": "Item1", "2": "Item2", "3": "Item3"} instead of ...