PHP生成json

索引数组

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

关联数组

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

json数组

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

json关联数组

<?php
$animal = array();
$animal['east'] = "tiger";
$animal['north'] = "wolf";
echo json_encode($animal); //{"east":"tiger","north":"wolf"}
?>
posted @ 2018-08-13 10:16  thomas_blog  阅读(318)  评论(0编辑  收藏  举报