0

I am pulling some JSON data from google maps that gives me an accuret distance between two addresses. That works fine but when it comes to using json_decode it gives me some horrific data! This is the example array thats output:

array(4) {
  ["destination_addresses"]=>
  array(1) {
    [0]=>
    string(51) "96 Stirling Street, Alva, Clackmannanshire FK12, UK"
  }
  ["origin_addresses"]=>
  array(1) {
    [0]=>
    string(64) "17 Dalgety Road, Edinburgh, Edinburgh, City of Edinburgh EH7, UK"
  }
  ["rows"]=>
  array(1) {
    [0]=>
    array(1) {
      ["elements"]=>
      array(1) {
        [0]=>
        array(3) {
          ["distance"]=>
          array(2) {
            ["text"]=>
            string(7) "39.3 mi"
            ["value"]=>
            int(63242)
          }
          ["duration"]=>
          array(2) {
            ["text"]=>
            string(7) "59 mins"
            ["value"]=>
            int(3555)
          }
          ["status"]=>
          string(2) "OK"
        }
      }
    }
  }
  ["status"]=>
  string(2) "OK"
}

AS you can see it also puts string(#) or int(#) at the start of the array item and " around the value. Whats causing this issues? Heres the outputted JSON.

{
   "destination_addresses" : [ "96 Stirling Street, Alva, Clackmannanshire FK12, UK" ],
   "origin_addresses" : [ "17 Dalgety Road, Edinburgh, Edinburgh, City of Edinburgh EH7, UK" ],
   "rows" : [
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "39.3 mi",
                  "value" : 63242
               },
               "duration" : {
                  "text" : "59 mins",
                  "value" : 3555
               },
               "status" : "OK"
            }
         ]
      }
   ],
   "status" : "OK"
}

My php code:

$url = fopen("https://maps.googleapis.com/maps/api/distancematrix/json?origins=".$latitudeFrom.",".$longitudeFrom."&destinations=".$latitudeTo.",".$longitudeTo."&mode=driving&units=imperial", "r");

        $result = json_decode(stream_get_contents($url), true);
        fclose($url);
        var_dump($result['rows'][0]['elements'][0]['distance']['text']);
2
  • That looks like the output of var_dump, which is great for debugging, not so good for data exchange. Commented Nov 24, 2014 at 17:12
  • Yes but I i were to go: echo $array['rows'][0]['elements'][0]['distance']['test']; I would get 'string(7) "39.3 mi"' and not '39.3 mi' as expected! Commented Nov 24, 2014 at 17:14

1 Answer 1

1

You're getting an object back, not an array. You need to reference it as such. The data you're seeing from var_dump will not be in the output when you handle the data. That is just for debugging purposes so you can see what you're dealing with.

json_decode with object returned

$json = json_decode($json);
echo $json->rows[0]->elements[0]->distance->text;

json_decode with array returned

$json = json_decode($json, true);
echo $json['rows'][0]['elements'][0]['distance']['text'];
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks but that still gives me the same issue. I have added my code to my first post.
@Jiggles don't var_dump use echo instead. var_dump is going to give you the extra info about that variable. echo will just give the value that's it.
@slapyo Not necessarily, I have currently issues that json_decode gives me json object AND the int at the end. echo and var_dump say the same thing

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.