How to generate a json file?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
33 views
Hello! For example there is contents of the json
{ "features": [{ "type": "Feature", "geometry": { "type": "Point", "coordinates": [55.831903, 37.411961] } } ] }

All this generated so
$json = json_decode(file_get_contents('data.json'), true); $json['features'][] = [ 'type' => 'Feature', 'geometry' => [ 'type' => 'Point', 'coordinates' => [55.831903,37.411961] ], ]; file_put_contents('data.json', json_encode($json));


All is working well. Ie PR change of coordinates and run the code above, added a new array of data and looks like this
{ "features": [ { "type": "Feature", "geometry": { "type": "Point", "coordinates": [55.831903, 37.411961] } }, { "type": "Feature", "geometry": { "type": "Point", "coordinates": [65.831903, 47.411961] } } ] }


Here needed to add before "features" to add "type": "FeatureCollection", but do not know how. To finally looked
{ "type": "FeatureCollection", "features": [ { "type": "Feature", "geometry": { "type": "Point", "coordinates": [55.831903, 37.411961] } }, { "type": "Feature", "geometry": { "type": "Point", "coordinates": [65.831903, 47.411961] } } ] }
by | 33 views

2 Answers

0 like 0 dislike
Are you kidding me? Learn the basics
spoiler
$json['type'] = 'asdasdasd';
by
0 like 0 dislike
$json['type'] = 'FeatureCollection';
before
file_put_contents('data.json', json_encode($json));

for example
by

Related questions

0 like 0 dislike
2 answers
asked May 20, 2019 by EvgMul
0 like 0 dislike
1 answer
asked Apr 12, 2019 by your_uncle
0 like 0 dislike
1 answer
asked Apr 9, 2019 by hooliganus
0 like 0 dislike
4 answers
110,608 questions
257,187 answers
0 comments
40,796 users