Through the API, I get data from a third-party service. Response structure:

[ { ... “Latitude”: “<Широта>”, “Longitude”: “<Долгота>”, ... } ] 

With the help of the php file getdata.php I pass through the array and collect all the data

 foreach ($response as $item) { $a = "[".$item['Latitude'].",".$item['Longitude']."]," ; $array = $array.$a ; } $list= substr($array, 0, -1); echo json_encode($list); 

This is a list of points that I want to display on the map using the Yandex.Maps API.

 var myMap = new ymaps.Map('map', { ... points = [ [55.831903,37.411961], [55.763338,37.565466] ], } 
  1. How do I transfer the data to the $ list variable in points ?
  2. Question about the correctness of the code in php? It seems to print the correct data, but is it necessary to do json_encode and is it possible to replace substr ($ array, 0, -1); on something inside foreach ?
  • if the js and php code in the same file can be done like this: var value = <?php echo $string;?> - Bogdan Gudyma
  • Yes. I know that. that's why I wrote that they are in different files - angelzzz
  • Make an Ajax request in getdata.php, and pass the data you need. - Bogdan Gudyma
  • function showData () {$ .ajax ({url: "/getdata.php", data: "", dataType: 'json', success: {points = $ list}}); return false; } - so does it work? - angelzzz
  • Yes, just correct the syntax. why do you return false in the function? - Bogdan Gudyma

1 answer 1

Make Ajax request to your file:

  function shaowData(){ var data = ''; $.ajax({ url: урл на getdata.php, dataType: 'json', async:false, success: function(response) { var data = response; } }); return data; } var points = showData(); // это будет ваш массив, дальше вставляете его куда вам нужно 

in the getdata.php file

  `foreach ($response as $item) { $a = "[".$item['Latitude'].",".$item['Longitude']."]," ; $array = $array.$a ; } $list= substr($array, 0, -1); echo json_encode($list);` 
  • instead of response, you can use the variable $ list? and how to assign its data in js variable points? - angelzzz
  • in the getdata file, do echo $list , and after the request response, it will be your array - Bogdan Gudyma
  • success: function(response) { где response ваш массив с данными. } success: function(response) { где response ваш массив с данными. } don’t understand what to write here and how to assign the value of the $ list variable. I write: function showData() { $.ajax({ url: "/PickpointapiYaMapGetPostamatlist.php", data: "", dataType: 'json', success: function(response) { var response = $list } }); } function showData() { $.ajax({ url: "/PickpointapiYaMapGetPostamatlist.php", data: "", dataType: 'json', success: function(response) { var response = $list } }); } writes Uncaught ReferenceError: $ list is not defined - angelzzz
  • @angelzzz, no, after the response request is your $ list, console.log (response), do it in the success function, if everything is correct you see your array - Bogdan Gudyma
  • console log displays the data. and now how to transfer them to the points variable here var myMap = new ymaps.Map('map', { ... points = [ [55.831903,37.411961], [55.763338,37.565466] ], } - angelzzz