0

I've the following PHP code with a JSON variable...

$route_geometry_json = "{
    \"route_geometry\": [
        [44.911537, 7.671326],
        [44.911481, 7.671462],
        [44.911455, 7.671531],
        [44.911434, 7.671602],
        [44.911358, 7.671859],
        [44.911273, 7.672175],
        [44.911198, 7.672458],
        [44.91113, 7.672617],
        [44.911069, 7.67275],
        [44.911003, 7.672821],
        [44.910945, 7.672881],
        [44.910869, 7.672954],
        [44.910868, 7.673046],
        [44.91091, 7.673109],
        [44.91095, 7.67319],
        [44.910964, 7.673266],
        [44.910958, 7.673407],
        [44.910955, 7.6735],
        [44.910947, 7.673632],
        [44.910922, 7.673871],
        [44.910828, 7.674786],
        [44.910711, 7.675816],
        [44.910606, 7.676364],
        [44.910467, 7.676322],
        [44.910368, 7.676308],
        [44.910051, 7.676253],
        [44.9097, 7.676162],
        [44.90944, 7.676041],
        [44.909297, 7.675958],
        [44.909174, 7.67583],
        [44.909107, 7.675722],
        [44.908993, 7.675583],
        [44.908758, 7.675448],
        [44.90796, 7.675037]
    ]
  }";

print "Route geometry -->" + json_encode($route_geometry_json);

The print return "0": any suggestion / example?

I'd like also to extract / print the coords couples like

    44.908993, 7.675583
    44.908758, 7.675448

Any suggestion will be appreciated ...

Thanks

Cesare

1
  • yep, encode converts a value to json, while decode converts a json to a variable Commented Mar 31, 2016 at 12:12

2 Answers 2

1

Use this code:

$route_geometry = json_decode($route_geometry_json);

foreach ($route_geometry->route_geometry as $value) {
    echo $value[0].', '.$value[1].'<br />';
}
Sign up to request clarification or add additional context in comments.

Comments

1

You need to print_r(json_decode($route_geometry_json)) it and not json_encode

json_encode is for creating a JSON string. But since you already have a JSON string already, you need to Decode it to make it an Array/Object.

UPDATE

Your requirement

echo "Route geometry -->"; 
print_r(json_decode($route_geometry_json));

You cannot concat a String and an Object, so you were getting that Parse error.

9 Comments

Thanks! I've tried but the result is ..... "Notice: Object of class stdClass could not be converted to int ....." :-(
use print_r to print
I've tried to use "print_r" to print and now the error is "Parse error: syntax error, unexpected '"Route geometry -->"' (T_CONSTANT_ENCAPSED_STRING)" ....
remove the back slashes from the string and it should work ... "route_geometry" and use single quotes for the whole string. Try
Because you cannot concat, a string and a Object as one. So, print them differently.
|

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.