Home >Backend Development >PHP Tutorial >In-depth analysis of php json format control, _PHP tutorial

In-depth analysis of php json format control, _PHP tutorial

WBOY
WBOYOriginal
2016-07-12 09:02:45769browse

In-depth analysis of php json format control,

Regarding the issue of json, novice friends have come to me one after another to ask, for example, why I output {"1":" item1","2":"item2","3":"item3"} instead of ["item1","item2","item3"].

php array and js array

I use PHP 5.4 or above syntax here.

There are associative arrays and index arrays in php, for example:

<&#63;php
// 索引数组
$arr = ['item1', 'item2', 'item3'];
// 关联数组
$arr = [
 'name' => '张三',
 'age' => '22',
]; 

There is only one kind of array in js, which is the index array. Maybe you will say that you can use K/V key-value pairs to simulate associative arrays.

K/V key-value pair looks like it, but it does not have any array characteristics, so it will not be explained in detail here.

The json formats obtained after json_encoding the above php array are ["item1", "item2", "item3"] and {"name":"u5f20u4e09","age":"22"} respectively. The Chinese here has been converted to Unicode. If you insist on displaying Chinese, PHP 5.4 and later supports the JSON_UNESCAPED_UNICODE parameter. json_encode($arr, JSON_UNESCAPED_UNICODE) can get {"name":"Zhang San","age":"22" }, but it is highly not recommended to write like this.

What we get here are json strings in array and object formats under js, so why are these two types generated, or in other words, under what circumstances will object format be generated, and under what circumstances will array format be generated.

php array output format control

I have listed the general situations, just look at the code.

<&#63;php
$arr = [ // 不是 0 开始,会输出对象
 1 => 'item1',
 2 => 'item2',
 3 => 'item3',
];
echo "输出对象: ", json_encode($arr), "\n";
// 输出对象: {"1":"item1","2":"item2","3":"item3"}
$arr = [ // 连续索引,输出数组
 0 => 'item1',
 1 => 'item2',
 2 => 'item3',
];
echo "输出数组: ", json_encode($arr), "\n";
// 输出数组: ["item1","item2","item3"]
$arr = [ // 连续索引,输出数组 
 'item1',
 'item2',
 'item3',
];
echo "输出数组: ", json_encode($arr), "\n";
// 输出数组: ["item1","item2","item3"]
$arr = [ // 索引不连续,输出对象
 0 => 'item1',
 1 => 'item2',
 2 => 'item3',
 5 => 'item5',
];
echo "输出对象: ", json_encode($arr), "\n";
// 输出对象: {"0":"item1","1":"item2","2":"item3","5":"item5"}
$arr = [ // 包含关联索引,一定输出对象
 0 => 'item1',
 1 => 'item2',
 2 => 'item3',
 'other' => '其他字段'
];
echo "输出对象: ", json_encode($arr), "\n";
// 输出对象: {"0":"item1","1":"item2","2":"item3","other":"\u5176\u4ed6\u5b57\u6bb5"}
// 关联数组 + 索引数组 实例
$arr = [ // 关联数组
 'other' => '其他字段',
 'count' => 3, // 数组个数
 'list' => [ // 索引数组
  'item1',
  'item2',
  'item3',
 ],
];
echo "对象+数组: ", json_encode($arr), "\n";
// 对象+数组: {"other":"\u5176\u4ed6\u5b57\u6bb5","count":3,"list":["item1","item2","item3"]} 

In fact, the first type is a problem that many novice friends often encounter.

Because after the database is read out, they like to use the id as an index, and the id of the database does not start from 0. Take a look at this example.

$arr = $User->where($where)->find(); // 读取数据
$list = [];
foreach($arr as $key => $val) { // 遍历数组
 $list[$key] = [
  'name' => $val['name'],
  'age' => $val['age'],
 ];
}
$list['count'] = count($arr); // 其他属性
echo json_encode($list); // 输出 json
// {"1":{"name":"zhangsan","age":22},"2":{"name":"lisi","age":25},"count":2} 

The last one is a more commonly used way of writing. Use custom fields and arrays together to modify the example just now.

$arr = $User->where($where)->find(); // 读取数据
$list = [];
foreach($arr as $key => $val) { // 遍历数组
 $list['list'][] = [ // 修改这里
  'name' => $val['name'],
  'age' => $val['age'],
 ];
}
$list['count'] = count($arr); // 其他属性
echo json_encode($list); // 输出 json
// {"list":[{"name":"zhangsan","age":22},{"name":"lisi","age":25}],"count":2} 

I hope it will be helpful to everyone in learning about php json format control. Thank you for your continued support of the Bangkejia website.

Articles you may be interested in:

  • GBK’s page outputs the php function in JSON format
  • php Unlimited data JSON format and JS parsing
  • PHP Detailed explanation of JSON format data interaction example code
  • PHP implementation code for operating JSON format data
  • Analysis of PHP using curl to submit json format data
  • php using curl to send json format data example
  • How PHP determines whether it is in json format
  • How to define literal objects using json data format in PHP
  • How to convert PHP arrays into json format

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1084550.htmlTechArticleIn-depth analysis of php json format control. Regarding the issue of json, novice friends have come to me one after another to ask, such as Why do I output {"1":"item1","2":"item2","3":"item3"} instead of...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn