Heim > php教程 > php手册 > Hauptteil

phpjson格式控制

WBOY
Freigeben: 2016-07-06 13:30:58
Original
1358 Leute haben es durchsucht

关于 json 这个问题,陆陆续续有新手朋友找我问,比如为什么我输出的是 {1:item1,2:item2,3:item3} 而不是 [item1,item2,item3] 。 php数组 与 js数组 我这里用 php 5.4 以上语法表示。 php 里有关联数组和索引数组,例如: ?php // 索引数组 $arr = [ item1

关于 json 这个问题,陆陆续续有新手朋友找我问,比如为什么我输出的是 {"1":"item1","2":"item2","3":"item3"} 而不是 ["item1","item2","item3"]

php数组 与 js数组

我这里用 php 5.4 以上语法表示。

php 里有关联数组和索引数组,例如:

<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">&#39;item1&#39;</span><span class="ot">,</span> <span class="st">&#39;item2&#39;</span><span class="ot">,</span> <span class="st">&#39;item3&#39;</span><span class="ot">];</span>

<span class="co">// 关联数组</span>
<span class="kw">$arr</span> = <span class="ot">[</span>
  <span class="st">&#39;name&#39;</span> => <span class="st">&#39;张三&#39;</span><span class="ot">,</span>
  <span class="st">&#39;age&#39;</span> => <span class="st">&#39;22&#39;</span><span class="ot">,</span>
<span class="ot">];</span></code>
Nach dem Login kopieren

而 js 里只有一种数组,那就是索引数组,也许你会说可以用 K/V 键值对形式模拟关联数组啊。
K/V 键值对看起来像,但他没有任何数组特性,这里就不详细说明了。

而上面的 php 数组 json_encode 后得到的 json 格式分别是 ["item1","item2","item3"]{"name":"\u5f20\u4e09","age":"22"}。这里的中文被转为 Unicode 了,如果你非要显示中文,php 5.4 之后支持 JSON_UNESCAPED_UNICODE 参数,json_encode($arr, JSON_UNESCAPED_UNICODE) 即可得到 {"name":"张三","age":"22"},不过非常不推荐这样写。

这里分别得到的是 js 下的 数组对象 格式的 json 字符串,那为什么会生成这两种类型,或者说,什么情况会生成对象格式,什么情况会生成数组格式呢。

php 数组 输出格式控制

大致几种情况我都列出来了,直接看代码。

<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">&#39;item1&#39;</span><span class="ot">,</span>
  <span class="dv">2</span> => <span class="st">&#39;item2&#39;</span><span class="ot">,</span>
  <span class="dv">3</span> => <span class="st">&#39;item3&#39;</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">&#39;item1&#39;</span><span class="ot">,</span>
  <span class="dv">1</span> => <span class="st">&#39;item2&#39;</span><span class="ot">,</span>
  <span class="dv">2</span> => <span class="st">&#39;item3&#39;</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">&#39;item1&#39;</span><span class="ot">,</span>
  <span class="st">&#39;item2&#39;</span><span class="ot">,</span>
  <span class="st">&#39;item3&#39;</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">&#39;item1&#39;</span><span class="ot">,</span>
  <span class="dv">1</span> => <span class="st">&#39;item2&#39;</span><span class="ot">,</span>
  <span class="dv">2</span> => <span class="st">&#39;item3&#39;</span><span class="ot">,</span>
  <span class="dv">5</span> => <span class="st">&#39;item5&#39;</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">&#39;item1&#39;</span><span class="ot">,</span>
  <span class="dv">1</span> => <span class="st">&#39;item2&#39;</span><span class="ot">,</span>
  <span class="dv">2</span> => <span class="st">&#39;item3&#39;</span><span class="ot">,</span>
  <span class="st">&#39;other&#39;</span> => <span class="st">&#39;其他字段&#39;</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">&#39;other&#39;</span> => <span class="st">&#39;其他字段&#39;</span><span class="ot">,</span>
  <span class="st">&#39;count&#39;</span> => <span class="dv">3</span><span class="ot">,</span> <span class="co">// 数组个数</span>
  <span class="st">&#39;list&#39;</span> => <span class="ot">[</span> <span class="co">// 索引数组</span>
    <span class="st">&#39;item1&#39;</span><span class="ot">,</span>
    <span class="st">&#39;item2&#39;</span><span class="ot">,</span>
    <span class="st">&#39;item3&#39;</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>
Nach dem Login kopieren

其实第一种就是很多新手朋友经常遇到的问题。
因为数据库读出来后他们喜欢把 id 当索引用,而数据库的 id 不是从 0 开始的,看下这个例子。

<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">&#39;name&#39;</span> => <span class="kw">$val</span><span class="ot">[</span><span class="st">&#39;name&#39;</span><span class="ot">],</span>
    <span class="st">&#39;age&#39;</span> => <span class="kw">$val</span><span class="ot">[</span><span class="st">&#39;age&#39;</span><span class="ot">],</span>
  <span class="ot">];</span>
}

<span class="kw">$list</span><span class="ot">[</span><span class="st">&#39;count&#39;</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>
Nach dem Login kopieren

而最后一种是比较常用的写法,自定义字段和数组一起用,来修改下刚才例子。

<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">&#39;list&#39;</span><span class="ot">][]</span> = <span class="ot">[</span> <span class="co">// 修改这里</span>
    <span class="st">&#39;name&#39;</span> => <span class="kw">$val</span><span class="ot">[</span><span class="st">&#39;name&#39;</span><span class="ot">],</span>
    <span class="st">&#39;age&#39;</span> => <span class="kw">$val</span><span class="ot">[</span><span class="st">&#39;age&#39;</span><span class="ot">],</span>
  <span class="ot">];</span>
}

<span class="kw">$list</span><span class="ot">[</span><span class="st">&#39;count&#39;</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>
Nach dem Login kopieren

OK,希望对刚入门的 phper 有所帮助。


Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage