Hello, there is a line coming with PHP in JS.

var locs=`[{"0":0,"info":"249","lat":"50.908","lng":"34.798"},{"1":1,"info":"400","lat":"50.908","lng":"34.798"}]`; 

Need a view in js:

 var locs = { "1": {"info": "11111. Some random info here", "lat": "-37.8139", "lng": "144.9634"}, "2": {"info": "22222. Some random info here", "lat": "46.0553", "lng": "14.5144"}, "3": {"info": "33333. Some random info here", "lat": "-33.7333", "lng": "151.0833"}, "4": {"info": "44444. Some random info here", "lat": "7.9798", "lng": "-81.731"} }; 

Now json_decode () gives the following form:

 Array ( [0] => stdClass Object ( [0] => 0 [info] => 249 [lat] => 50.908 [lng] => 34.798 ) [1] => stdClass Object ( [1] => 1 [info] => 400 [lat] => 50.908 [lng] => 34.798 ) ) 

How to make proper array look like in PHP? To complete the picture I attach a screenshot: enter image description here

  • See the second argument (parameter) of the json_decode function set to true and get an array, not an object and specify the recursion depth with 3 arguments (in your case 2). And you don’t have to drag out the pictures, let's take less working examples, nobody will rewrite for you. - And
  • thank. the picture simply duplicates the code in order to see the context of the question - Semen

1 answer 1

In PHP, there is no such thing that in JS it will normally be interpreted as "Object", stdClass is just a wrapper for accessing fields. And it is not convenient for work, unnatural for PHP

Tip 1. Use json_decode($string, true); if parsing JSON in PHP - the second parameter true indicates the need to represent the JavaScript Object in the form of ordinary associative PHP arrays, and not in the form of stdClass. It’s much easier to work with them than with stdClass, sorting fields, checking availability and so on - everything becomes very simple

Tip 2. Do not complicate your life with witchcraft with stdClass in PHP. Use ordinary associative arrays. If the array keys are string, it will be converted to Object:

 $locs = array( "1" => array( "info" => "11111. Some random info here", "lat" => "-37.8139", "lng" => "144.9634" ), "2" => array( "info" => "22222. Some random info here", "lat" => "46.0553", "lng" => "14.5144" ), "3" => array( "info" => "33333. Some random info here", "lat" => "-33.7333", "lng" => "151.0833" ), "4" => array( "info" => "44444. Some random info here", "lat" => "7.9798", "lng" => "-81.731" ) ); print json_encode($locs); /* * JSON-строка: { "1":{"info":"11111. Some random info here","lat":"-37.8139","lng":"144.9634"}, "2":{"info":"22222. Some random info here","lat":"46.0553","lng":"14.5144"}, "3":{"info":"33333. Some random info here","lat":"-33.7333","lng":"151.0833"}, "4":{"info":"44444. Some random info here","lat":"7.9798","lng":"-81.731"} } */ 

Good luck! PHP is beautiful with its only "array" for all occasions, which can do everything at once, and not a zoo of entities like "tuples", "hashes", "dictionaries" and other heresy))

  • Thank you!) It helped - Semen
  • Yes, pay attention to the string keys "1", "2", "3" and "4". If they start with 0 and are numbers, then PHP segens the JS array like [0:{}, 1: {}, 2:{}...] -> [{}, {}, {}, {}...] , and this is not what you need. If you generate such a piece of code, you can use the trick with strval() for keys, the array will be reassembled - wirtwelt
  • Yes, I generate the code. Only strval does not help. What am I doing wrong? - Semyon
  • prnt.sc/j4nzj9 - available
  • Already understood) translated in json on the client and it all worked - semen