Home  >  Article  >  Backend Development  >  How does php generate json? PHP method code to generate json

How does php generate json? PHP method code to generate json

不言
不言Original
2018-08-13 15:04:184770browse

本篇文章给大家带来的内容是关于php如何生成json?php生成json的方法代码,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

索引数组

<?php
//json_encode(数组/对象)
$color = array(&#39;red&#39;,&#39;blue&#39;,&#39;green&#39;);
echo json_encode($color); //["red","blue","green"]
?>

关联数组

<?php
$animal = array(&#39;east&#39;=>&#39;tiger&#39;,&#39;north&#39;=>&#39;wolf&#39;,&#39;south&#39;=>&#39;monkey&#39;);
echo json_encode($animal); //{"east":"tiger","north":"wolf","south":"monkey"}
?>

json数组

<?php
$file_arr = array();//保存文件名
$dir = dir("D:\\input");//目录
while($file = $dir->read()){//循环目录
    if($file !=".." && $file !="."){//判断不为返回上级
        $file_arr[] = $file;
    }
}echo json_encode($file_arr); //["1.png","2.png"]
?>

相关推荐:

用php怎么获取文件的大小进行判断?(代码示例)

PHP中是如何声明多个变量的?(多种方法介绍)

The above is the detailed content of How does php generate json? PHP method code to generate json. 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