json文件的创建、获取
function jsonFile(){
$list = Db::name("table")->field("name,coords,img_src,description,link")->where("status",1)->select();
$arrData= [];
foreach ($list as $v){
if($v["img_src"]){
$imgSrc = $v["img_src"];
}else{
$imgSrc = "/static/image/y.jpg";
}
$line = ["type"=> "Feature",
"properties"=> ["name"=>$v["name"], "description"=> $v["description"], "link"=> $v["link"], "img"=> $imgSrc ],
"geometry"=>[ "type"=>"Point", "coordinates"=> explode(",",$v[ 'coords']) ]
];
array_push($arrData,$line);
}
$collateData =["type"=> "FeatureCollection","features"=>[$arrData]];
$jstring = json_encode($collateData);
// 创建json文件并将数据写入文件
file_put_contents("static/data.json", $jstring);
// 获取json文件数据
$geijson = file_get_contents("static/data.json");
// 把JSON字符串转成PHP数组
$res= json_decode($geijson , true);
// 显示出来
print_r($res);
}
如果复杂一点就把我整不会了怎么办?凉拌呗
|