Hello!

String .json:

{ "type": "FeatureCollection", "features": [ { "type": "Feature", "id": 1, "options": {"strokeWidth": 3}, "properties": {"balloonContent": "Π‘ΠΎΠ΄Π΅Ρ€ΠΆΠΈΠΌΠΎΠ΅ Π±Π°Π»ΡƒΠ½Π°", "hintContent": "ВСкст подсказки"}, "geometry": { "type": "LineString", "coordinates": [ [ 55.80899899270488, 37.77442932128906 ], [ 55.800895029938275, 37.74696350097656 ], [ 55.795877445664104, 37.709197998046875 ] ] } } ] } 

The user enters the coordinates in the field, for example: [[00.0000, 00.0000], [11.0000, 11.0000], [22.0000. 22,0000]]

Further php code:

 <?php $json = file_get_contents('data.json'); $data = json_decode($json, true); $coordinates = $_POST['coordinates']; $arr['geometry']['coordinates'] = $coordinates; array_push($data['features'], $arr); $json = json_encode($data, JSON_UNESCAPED_UNICODE); file_put_contents('data.json', $json); ?> 

After this, the entry in .json looks like this:

  "coordinates": "[ [ 55.80899899270488, 37.77442932128906 ], [ 55.800895029938275, 37.74696350097656 ], [ 55.795877445664104, 37.709197998046875 ] ]" 

And you need to be so (without quotes):

  "coordinates": [ [ 55.80899899270488, 37.77442932128906 ], [ 55.800895029938275, 37.74696350097656 ], [ 55.795877445664104, 37.709197998046875 ] ] 

Tell me, please, what is the error? How to fix? Thank.

  • You have already come to $ _POST ['coordinates'] json Or what? - Moonvvell
  • the variable is taken from <input>, the coordinates are entered by the user in the set format [[00.0000, 00.0000], [11.0000, 11.0000], [22.0000. 22,0000]] - XFlame
  • one
    A string was sent, a string and added. Decode json_decode what you received from the user, i.e. . json_decode($coordinates) - teran

1 answer 1

 $coordinates = json_decode ($_POST['coordinates'], true); 

Or you need to make a more complex set of input through html. Or via JS input, save to hidden fields. The name attribute for the fields in this case will look something like this:

  • coordinates [0] []
  • coordinates [0] []
  • coordinates [1] []
  • coordinates [1] []
  • coordinates [2] []
  • coordinates [2] []