Home  >  Article  >  Backend Development  >  Detailed explanation of json format control in php

Detailed explanation of json format control in php

*文
*文Original
2017-12-27 17:58:461578browse

JSON is widely used in data transmission due to its high-performance transmission and analysis. This article mainly introduces relevant information on in-depth analysis of php json format control. I hope to be helpful.

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 the above syntax of php 5.4 here.

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


##

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


and js There is only one kind of array in there, and that 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":"\u5f20\u4e09","age ":"twenty two"}. The Chinese here has been converted to Unicode. If you insist on displaying Chinese, PHP 5.4 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 arrays under js and json strings in object format. So why are these two types generated? In other words, under what circumstances will object format be generated and under what circumstances will arrays be generated? What about the format.


php Array Output Format Control

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


<?php
$arr = [ // 不是 0 开始,会输出对象
 1 => &#39;item1&#39;,
 2 => &#39;item2&#39;,
 3 => &#39;item3&#39;,
];
echo "输出对象: ", json_encode($arr), "\n";
// 输出对象: {"1":"item1","2":"item2","3":"item3"}
$arr = [ // 连续索引,输出数组
 0 => &#39;item1&#39;,
 1 => &#39;item2&#39;,
 2 => &#39;item3&#39;,
];
echo "输出数组: ", json_encode($arr), "\n";
// 输出数组: ["item1","item2","item3"]
$arr = [ // 连续索引,输出数组 
 &#39;item1&#39;,
 &#39;item2&#39;,
 &#39;item3&#39;,
];
echo "输出数组: ", json_encode($arr), "\n";
// 输出数组: ["item1","item2","item3"]
$arr = [ // 索引不连续,输出对象
 0 => &#39;item1&#39;,
 1 => &#39;item2&#39;,
 2 => &#39;item3&#39;,
 5 => &#39;item5&#39;,
];
echo "输出对象: ", json_encode($arr), "\n";
// 输出对象: {"0":"item1","1":"item2","2":"item3","5":"item5"}
$arr = [ // 包含关联索引,一定输出对象
 0 => &#39;item1&#39;,
 1 => &#39;item2&#39;,
 2 => &#39;item3&#39;,
 &#39;other&#39; => &#39;其他字段&#39;
];
echo "输出对象: ", json_encode($arr), "\n";
// 输出对象: {"0":"item1","1":"item2","2":"item3","other":"\u5176\u4ed6\u5b57\u6bb5"}
// 关联数组 + 索引数组 实例
$arr = [ // 关联数组
 &#39;other&#39; => &#39;其他字段&#39;,
 &#39;count&#39; => 3, // 数组个数
 &#39;list&#39; => [ // 索引数组
  &#39;item1&#39;,
  &#39;item2&#39;,
  &#39;item3&#39;,
 ],
];
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] = [
  &#39;name&#39; => $val[&#39;name&#39;],
  &#39;age&#39; => $val[&#39;age&#39;],
 ];
}
$list[&#39;count&#39;] = 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 common 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[&#39;list&#39;][] = [ // 修改这里
  &#39;name&#39; => $val[&#39;name&#39;],
  &#39;age&#39; => $val[&#39;age&#39;],
 ];
}
$list[&#39;count&#39;] = count($arr); // 其他属性
echo json_encode($list); // 输出 json
// {"list":[{"name":"zhangsan","age":22},{"name":"lisi","age":25}],"count":2}



##Related recommendations:

PHP solves the JSON Chinese display problem

php converts the index array into json

Introduction to usage examples of json_encode in php

The above is the detailed content of Detailed explanation of json format control in php. For more information, please follow other related articles on the PHP Chinese website!

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