0

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 converted 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

4 Answers 4

5

Try...

foreach($markers as $marker) 
{
     $some_string .= '('.$marker->lat.','.$marker->lng.')';
}

Result...

(37.7903882619,-122.460479968)(37.7896082315,-122.463441127)
Sign up to request clarification or add additional context in comments.

Comments

3
foreach($markers as $key => $value) 
{
    // $value is object with lat and lng properties 
    $some_string = $some_string.' ( lat:'.$value->lat.' ) ';
}

Comments

1

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.

Comments

0

This here works

var json = JSON.parse(<?php echo "'" . stripslashes($_POST['json']) . "'" ?>);

1 Comment

This really doesn't address question better than any of the other answers do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.