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 arrays and JS arrays are represented here using the above syntax of PHP 5.4. There are associative arrays and index arrays in php, for example: ?php // Index array $arr = [ item1
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"]
.
I use PHP 5.4 or above syntax here.
There are associative arrays and index arrays in php, for example:
<code class="sourceCode php"><span class="kw"><?php</span> <span class="co">// 索引数组</span> <span class="kw">$arr</span> = <span class="ot">[</span><span class="st">'item1'</span><span class="ot">,</span> <span class="st">'item2'</span><span class="ot">,</span> <span class="st">'item3'</span><span class="ot">];</span> <span class="co">// 关联数组</span> <span class="kw">$arr</span> = <span class="ot">[</span> <span class="st">'name'</span> => <span class="st">'张三'</span><span class="ot">,</span> <span class="st">'age'</span> => <span class="st">'22'</span><span class="ot">,</span> <span class="ot">];</span></code>
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 pairs look like it, but they don't have any array properties, so they won't be explained in detail here.
The json formats obtained after the above php array json_encode
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, and json_encode($arr, JSON_UNESCAPED_UNICODE)
can get {"name":"张三","age":"22"}
, but it is highly not recommended to write like this.
What we get here are the json strings in the format of array and object under js respectively. So why are these two types generated? In other words, under what circumstances will the object format be generated? , under what circumstances will the array format be generated?
I have listed the general situations, just look at the code.
<code class="sourceCode php"><span class="kw"><?php</span> <span class="kw">$arr</span> = <span class="ot">[</span> <span class="co">// 不是 0 开始,会输出对象</span> <span class="dv">1</span> => <span class="st">'item1'</span><span class="ot">,</span> <span class="dv">2</span> => <span class="st">'item2'</span><span class="ot">,</span> <span class="dv">3</span> => <span class="st">'item3'</span><span class="ot">,</span> <span class="ot">];</span> <span class="fu">echo</span> <span class="st">"输出对象: "</span><span class="ot">,</span> <span class="fu">json_encode</span><span class="ot">(</span><span class="kw">$arr</span><span class="ot">),</span> <span class="st">"</span><span class="kw">\n</span><span class="st">"</span><span class="ot">;</span> <span class="co">// 输出对象: {"1":"item1","2":"item2","3":"item3"}</span> <span class="kw">$arr</span> = <span class="ot">[</span> <span class="co">// 连续索引,输出数组</span> <span class="dv">0</span> => <span class="st">'item1'</span><span class="ot">,</span> <span class="dv">1</span> => <span class="st">'item2'</span><span class="ot">,</span> <span class="dv">2</span> => <span class="st">'item3'</span><span class="ot">,</span> <span class="ot">];</span> <span class="fu">echo</span> <span class="st">"输出数组: "</span><span class="ot">,</span> <span class="fu">json_encode</span><span class="ot">(</span><span class="kw">$arr</span><span class="ot">),</span> <span class="st">"</span><span class="kw">\n</span><span class="st">"</span><span class="ot">;</span> <span class="co">// 输出数组: ["item1","item2","item3"]</span> <span class="kw">$arr</span> = <span class="ot">[</span> <span class="co">// 连续索引,输出数组 </span> <span class="st">'item1'</span><span class="ot">,</span> <span class="st">'item2'</span><span class="ot">,</span> <span class="st">'item3'</span><span class="ot">,</span> <span class="ot">];</span> <span class="fu">echo</span> <span class="st">"输出数组: "</span><span class="ot">,</span> <span class="fu">json_encode</span><span class="ot">(</span><span class="kw">$arr</span><span class="ot">),</span> <span class="st">"</span><span class="kw">\n</span><span class="st">"</span><span class="ot">;</span> <span class="co">// 输出数组: ["item1","item2","item3"]</span> <span class="kw">$arr</span> = <span class="ot">[</span> <span class="co">// 索引不连续,输出对象</span> <span class="dv">0</span> => <span class="st">'item1'</span><span class="ot">,</span> <span class="dv">1</span> => <span class="st">'item2'</span><span class="ot">,</span> <span class="dv">2</span> => <span class="st">'item3'</span><span class="ot">,</span> <span class="dv">5</span> => <span class="st">'item5'</span><span class="ot">,</span> <span class="ot">];</span> <span class="fu">echo</span> <span class="st">"输出对象: "</span><span class="ot">,</span> <span class="fu">json_encode</span><span class="ot">(</span><span class="kw">$arr</span><span class="ot">),</span> <span class="st">"</span><span class="kw">\n</span><span class="st">"</span><span class="ot">;</span> <span class="co">// 输出对象: {"0":"item1","1":"item2","2":"item3","5":"item5"}</span> <span class="kw">$arr</span> = <span class="ot">[</span> <span class="co">// 包含关联索引,一定输出对象</span> <span class="dv">0</span> => <span class="st">'item1'</span><span class="ot">,</span> <span class="dv">1</span> => <span class="st">'item2'</span><span class="ot">,</span> <span class="dv">2</span> => <span class="st">'item3'</span><span class="ot">,</span> <span class="st">'other'</span> => <span class="st">'其他字段'</span> <span class="ot">];</span> <span class="fu">echo</span> <span class="st">"输出对象: "</span><span class="ot">,</span> <span class="fu">json_encode</span><span class="ot">(</span><span class="kw">$arr</span><span class="ot">),</span> <span class="st">"</span><span class="kw">\n</span><span class="st">"</span><span class="ot">;</span> <span class="co">// 输出对象: {"0":"item1","1":"item2","2":"item3","other":"\u5176\u4ed6\u5b57\u6bb5"}</span> <span class="co">// 关联数组 + 索引数组 实例</span> <span class="kw">$arr</span> = <span class="ot">[</span> <span class="co">// 关联数组</span> <span class="st">'other'</span> => <span class="st">'其他字段'</span><span class="ot">,</span> <span class="st">'count'</span> => <span class="dv">3</span><span class="ot">,</span> <span class="co">// 数组个数</span> <span class="st">'list'</span> => <span class="ot">[</span> <span class="co">// 索引数组</span> <span class="st">'item1'</span><span class="ot">,</span> <span class="st">'item2'</span><span class="ot">,</span> <span class="st">'item3'</span><span class="ot">,</span> <span class="ot">],</span> <span class="ot">];</span> <span class="fu">echo</span> <span class="st">"对象+数组: "</span><span class="ot">,</span> <span class="fu">json_encode</span><span class="ot">(</span><span class="kw">$arr</span><span class="ot">),</span> <span class="st">"</span><span class="kw">\n</span><span class="st">"</span><span class="ot">;</span> <span class="co">// 对象+数组: {"other":"\u5176\u4ed6\u5b57\u6bb5","count":3,"list":["item1","item2","item3"]}</span></code>
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.
<code class="sourceCode php"><span class="kw">$arr</span> = <span class="kw">$User</span>->where<span class="ot">(</span><span class="kw">$where</span><span class="ot">)</span>->find<span class="ot">();</span> <span class="co">// 读取数据</span> <span class="kw">$list</span> = <span class="ot">[];</span> <span class="kw">foreach</span><span class="ot">(</span><span class="kw">$arr</span> <span class="kw">as</span> <span class="kw">$key</span> => <span class="kw">$val</span><span class="ot">)</span> { <span class="co">// 遍历数组</span> <span class="kw">$list</span><span class="ot">[</span><span class="kw">$key</span><span class="ot">]</span> = <span class="ot">[</span> <span class="st">'name'</span> => <span class="kw">$val</span><span class="ot">[</span><span class="st">'name'</span><span class="ot">],</span> <span class="st">'age'</span> => <span class="kw">$val</span><span class="ot">[</span><span class="st">'age'</span><span class="ot">],</span> <span class="ot">];</span> } <span class="kw">$list</span><span class="ot">[</span><span class="st">'count'</span><span class="ot">]</span> = <span class="fu">count</span><span class="ot">(</span><span class="kw">$arr</span><span class="ot">);</span> <span class="co">// 其他属性</span> <span class="fu">echo</span> <span class="fu">json_encode</span><span class="ot">(</span><span class="kw">$list</span><span class="ot">);</span> <span class="co">// 输出 json</span> <span class="co">// {"1":{"name":"zhangsan","age":22},"2":{"name":"lisi","age":25},"count":2}</span></code>
The last one is a more commonly used way of writing. Use custom fields and arrays together to modify the example just now.
<code class="sourceCode php"><span class="kw">$arr</span> = <span class="kw">$User</span>->where<span class="ot">(</span><span class="kw">$where</span><span class="ot">)</span>->find<span class="ot">();</span> <span class="co">// 读取数据</span> <span class="kw">$list</span> = <span class="ot">[];</span> <span class="kw">foreach</span><span class="ot">(</span><span class="kw">$arr</span> <span class="kw">as</span> <span class="kw">$key</span> => <span class="kw">$val</span><span class="ot">)</span> { <span class="co">// 遍历数组</span> <span class="kw">$list</span><span class="ot">[</span><span class="st">'list'</span><span class="ot">][]</span> = <span class="ot">[</span> <span class="co">// 修改这里</span> <span class="st">'name'</span> => <span class="kw">$val</span><span class="ot">[</span><span class="st">'name'</span><span class="ot">],</span> <span class="st">'age'</span> => <span class="kw">$val</span><span class="ot">[</span><span class="st">'age'</span><span class="ot">],</span> <span class="ot">];</span> } <span class="kw">$list</span><span class="ot">[</span><span class="st">'count'</span><span class="ot">]</span> = <span class="fu">count</span><span class="ot">(</span><span class="kw">$arr</span><span class="ot">);</span> <span class="co">// 其他属性</span> <span class="fu">echo</span> <span class="fu">json_encode</span><span class="ot">(</span><span class="kw">$list</span><span class="ot">);</span> <span class="co">// 输出 json</span> <span class="co">// {"list":[{"name":"zhangsan","age":22},{"name":"lisi","age":25}],"count":2}</span></code>
OK, I hope it will be helpful to those who are just getting started with phper.