小编Ran*_*dom的帖子

如何从PHP写入时从JSON文件中删除引用?

我目前有以下代码,它有一个数组,并将其输出到一个名为'example.json'的JSON文件中.

以下是输出到它的代码:

$x = array(1, 2, 3); //Defining two basic arrays
$y = array(2, 4, 6);
$name = array("Joe", "John", "Johnny");
echo count($x);
$objOne = '["type": "FeatureCollection", "features": [';
file_put_contents("jsonfun.json", json_encode($objOne));
for($i = 0; $i < count($x); $i++)
{
    $objTwo = '{ "type": "Feature", "geometry": {"type": "Point", "coordinates": [' . $x[$i] . ', ' . $y[$i] . ']}, "properties": {"name": ' . $name[$i] . '} }]';
    file_put_contents("jsonfun.json", json_encode($objTwo), FILE_APPEND);
}
$objThree = '};';
file_put_contents("jsonfun.json", json_encode($objThree), FILE_APPEND);
Run Code Online (Sandbox Code Playgroud)

输出:

"[\"type\": \"FeatureCollection\", \"features\": …
Run Code Online (Sandbox Code Playgroud)

php json

1
推荐指数
1
解决办法
39
查看次数

标签 统计

json ×1

php ×1