I had this string:
{\"lat\":37.790388261934424,\"lng\":-122.46047996826172},{\"lat\":37.789608231530124,\"lng\":-122.46344112701416}
Then thanks to the help of StackOverflowers, I did this to strip the slashes:
$markers = stripslashes($markers);
Then I tried to add the outside brackets which seemed to be needed by the decode function
$markers = json_decode('['.$markers.']');
Was that the right way to go about it? Then I tried to do this:
foreach($markers as $key => $value)
{
$some_string = $some_string.' ( '.$value.' ) ';
}
Which game me this error:
Object of class stdClass could not be conv开发者_JS百科erted to string
But what I really need is to extract the lat/lng values into $lat , $lng variables. Could someone suggest to me how to fix the error I am getting and put the values into the variables?
Thanks, Alex
Try...
foreach($markers as $marker)
{
$some_string .= '('.$marker->lat.','.$marker->lng.')';
}
Result...
(37.7903882619,-122.460479968)(37.7896082315,-122.463441127)
foreach($markers as $key => $value)
{
// $value is object with lat and lng properties
$some_string = $some_string.' ( lat:'.$value->lat.' ) ';
}
This was something that puzzled me too, why is json_decode() returning the data encapsulated in objects?
It turns out that all arrays in Javascript are considered objects, hence json_decode() returns them as objects of the PHP stdClass. Try to do print_r() on the $marker to see how it is structured.
This here works
var json = JSON.parse(<?php echo "'" . stripslashes($_POST['json']) . "'" ?>);
精彩评论