Error showing return of json array in php

Asked

Viewed 222 times

0

Error:

Notice: Trying to get Property of non-object in C: xampp htdocs json get_json.php on line 6

My php code:

<?php
$get = 'https://maps.googleapis.com/maps/api/place/autocomplete/json?&location=-18.866269,-41.9558537&radius=1&input=Rua%20Trin&types=address&language=pt_BR&key=dsdfdsfg21321erwr4rgf34gerg443g43hg34yh5t43tf3';

$VarJson = file_get_contents( $get );
        $obj = json_decode($VarJson, true);
        $VarRetorno =   $obj['predictions']['description']= $obj->description;
echo $VarRetorno;
?>

Json that is being returned:

{ "predictions" : [ { "description" : "Rua Trinta e Quatro - Lourdes, Governador Valadares - MG, Brasil", "id" : "17ba7fa54b2d700007d97209fef6d5d3a8ff72e0", "matched_substrings" : [ { "length" : 8, "offset" : 0 } ], "place_id" : "EkBSdWEgVHJpbnRhIGUgUXVhdHJvIC0gTG91cmRlcywgR292ZXJuYWRvciBWYWxhZGFyZXMgLSBNRywgQnJhc2ls", "reference" : "CnRuAAAALZ2UYdupWz6GPbFvSYk4GXtquB7-enDBfMTxQSsFk6rMcC1Rb5ThNRwZak8lSjfFYlsaamMFuX4AuOLrYl3cJKKMUNSSx8jpgHcUQIcqFdKVk76P6saacMHUU0SVzEm2WMcJ8KQf_G3GksdGN3hgFRIQ4nPnz6ZlFNwC6dCpdZPoRBoUQ4_hj9FdRZM3JitMgsnpEpKkhnE", "structured_formatting" : { "main_text" : "Rua Trinta e Quatro", "main_text_matched_substrings" : [ { "length" : 8, "offset" : 0 } ], "secondary_text" : "Lourdes, Governador Valadares - MG, Brasil" }, "terms" : [ { "offset" : 0, "value" : "Rua Trinta e Quatro" }, { "offset" : 22, "value" : "Lourdes" }, { "offset" : 31, "value" : "Governador Valadares" }, { "offset" : 54, "value" : "MG" }, { "offset" : 58, "value" : "Brasil" } ], "types" : [ "route", "geocode" ] }, { "description" : "Rua Trinta e Dois - Ilha dos Araújos, Governador Valadares - MG, Brasil", "id" : "e7ef14e0d420b70015b02e4e1a8a5e2172aaa46c", "matched_substrings" : [ { "length" : 8, "offset" : 0 } ], "place_id" : "EkhSdWEgVHJpbnRhIGUgRG9pcyAtIElsaGEgZG9zIEFyYcO6am9zLCBHb3Zlcm5hZG9yIFZhbGFkYXJlcyAtIE1HLCBCcmFzaWw", "reference" : "CoQBdgAAAA-RPo0LCQ99BPlVWsBGmtob1LDyZR3KyfOTNrS4-04eLv2qGxBy27dHq-XKVIFkOrg2Rhr539RTYSYZhmQuuyqDijrqH-H0d2DjixHTO-hOgNYZOkzluH9oKZ0yma5OcKVr5efU0AtA97MuuLkK8bGVx6yC-5Oy0tcII0kXOhv5EhBfsg7tNwh6kZxfMQry1JkDGhQCEmwOyXb2OaTK4Bx_Zqsn1lhRcA", "structured_formatting" : { "main_text" : "Rua Trinta e Dois", "main_text_matched_substrings" : [ { "length" : 8, "offset" : 0 } ], "secondary_text" : "Ilha dos Araújos, Governador Valadares - MG, Brasil" }, "terms" : [ { "offset" : 0, "value" : "Rua Trinta e Dois" }, { "offset" : 20, "value" : "Ilha dos Araújos" }, { "offset" : 38, "value" : "Governador Valadares" }, { "offset" : 61, "value" : "MG" }, { "offset" : 65, "value" : "Brasil" } ], "types" : [ "route", "geocode" ] }, { "description" : "Rua Trinta e Oito - Ilha dos Araújos, Governador Valadares - MG, Brasil", "id" : "8966aca97bad47745f86d8231068c026a71dbe40", "matched_substrings" : [ { "length" : 8, "offset" : 0 } ], "place_id" : "EkhSdWEgVHJpbnRhIGUgT2l0byAtIElsaGEgZG9zIEFyYcO6am9zLCBHb3Zlcm5hZG9yIFZhbGFkYXJlcyAtIE1HLCBCcmFzaWw", "reference" : "CoQBdgAAAAmej8mJkhc90gbtnqoXMHogn48EFqvq3wgzyo2KjvXAnuH1yHphO7lSuKUTOx7vSi-NyZodgp6W4ZsSztzpy6ZB8p-EHEti4Re_j3znnPSMRPnSI-R0Qd5YVCskghIBvacIrYw3x9KQAf8x89FONdK0T_khElzGJDoBNDdmhgwpEhCcPCe8W0FS5xaCLx7yDVh5GhS8IdOD7YTiKfBdxJPTApGWAQtb1A", "structured_formatting" : { "main_text" : "Rua Trinta e Oito", "main_text_matched_substrings" : [ { "length" : 8, "offset" : 0 } ], "secondary_text" : "Ilha dos Araújos, Governador Valadares - MG, Brasil" }, "terms" : [ { "offset" : 0, "value" : "Rua Trinta e Oito" }, { "offset" : 20, "value" : "Ilha dos Araújos" }, { "offset" : 38, "value" : "Governador Valadares" }, { "offset" : 61, "value" : "MG" }, { "offset" : 65, "value" : "Brasil" } ], "types" : [ "route", "geocode" ] }, { "description" : "Rua Trinta e Seis - Ilha dos Araújos, Governador Valadares - MG, Brasil", "id" : "23ac369477e2deb81bfbdd4629c694fe52d6dab4", "matched_substrings" : [ { "length" : 8, "offset" : 0 } ], "place_id" : "EkhSdWEgVHJpbnRhIGUgU2VpcyAtIElsaGEgZG9zIEFyYcO6am9zLCBHb3Zlcm5hZG9yIFZhbGFkYXJlcyAtIE1HLCBCcmFzaWw", "reference" : "CoQBdgAAAFp2zMZl1_vXoPN4X_lNeFfAC-sraI_P-nDbR95Nm9Z-gvyLnHRZZVYzz70nHkCf3M4euIYQa9w1DP6ijSAHz2ceOLkHOi2SwgLN9oUTwJG_EjoNb0BzUlWTQ4dbvGXanwQhayjkdFuWmAo1FjcGPR8R8fg8xakO-rcrvDCeX1lnEhCMEhrd5FT3MkUYwC8MNKVWGhQPsMzr3_uXevpCP1qP3d6CCH_5WQ", "structured_formatting" : { "main_text" : "Rua Trinta e Seis", "main_text_matched_substrings" : [ { "length" : 8, "offset" : 0 } ], "secondary_text" : "Ilha dos Araújos, Governador Valadares - MG, Brasil" }, "terms" : [ { "offset" : 0, "value" : "Rua Trinta e Seis" }, { "offset" : 20, "value" : "Ilha dos Araújos" }, { "offset" : 38, "value" : "Governador Valadares" }, { "offset" : 61, "value" : "MG" }, { "offset" : 65, "value" : "Brasil" } ], "types" : [ "route", "geocode" ] }, { "description" : "Rua Trinta de Janeiro - Nossa Senhora das Gracas, Governador Valadares - MG, Brasil", "id" : "83856debc2c5dd47d125aef6d7fa974529efc2ad", "matched_substrings" : [ { "length" : 8, "offset" : 0 } ], "place_id" : "ElNSdWEgVHJpbnRhIGRlIEphbmVpcm8gLSBOb3NzYSBTZW5ob3JhIGRhcyBHcmFjYXMsIEdvdmVybmFkb3IgVmFsYWRhcmVzIC0gTUcsIEJyYXNpbA", "reference" : "CpQBgQAAAC_ucSTthN0tdwSGzHbskEt2FzYbIPhWscDg01WK1IrBeIvVI11Z5TEjd0AYe3hu_qQwSaLBvj08PYcmijGdL3-Lxm2YNjG7KOgHuMZwA2RBS2wE5WjSitvGOrRHLXsQl8DOpGaM_YktBatGNuYFyhXAlyVQcw3lIx2Kcl3LkH5yKP_9__Lt_vh2aJBFpDTPBBIQacC0hSPxeUwwyEj9E3R3ixoUy848UB8MLecNG91G81CxbHrHFD0", "structured_formatting" : { "main_text" : "Rua Trinta de Janeiro", "main_text_matched_substrings" : [ { "length" : 8, "offset" : 0 } ], "secondary_text" : "Nossa Senhora das Gracas, Governador Valadares - MG, Brasil" }, "terms" : [ { "offset" : 0, "value" : "Rua Trinta de Janeiro" }, { "offset" : 24, "value" : "Nossa Senhora das Gracas" }, { "offset" : 50, "value" : "Governador Valadares" }, { "offset" : 73, "value" : "MG" }, { "offset" : 77, "value" : "Brasil" } ], "types" : [ "route", "geocode" ] } ], "status" : "OK" }

I only need to take the fields description

  • Appear to be an array, try $obj['predictions']['description'] = $obj['description'];

  • @rray Returned this error now: Notice: Undefined index: Description in C: xampp htdocs json get_json.php on line 6

  • Is that line right? has two assignments one of them doesn’t seem correct ( $obj->description) pq basically you take a json value and try to assign it to yourself. $VarRetorno = $obj['predictions']['description']= $obj->description;

2 answers

0

The "Predictions" field is an array so will have several "Description", if I understood what you want this should solve:

<?php
$get = 'https://maps.googleapis.com/maps/api/place/autocomplete/json?&location=-18.866269,-41.9558537&radius=1&input=Rua%20Trin&types=address&language=pt_BR&key=dsdfdsfg21321erwr4rgf34gerg443g43hg34yh5t43tf3';

$VarJson = file_get_contents( $get );
$obj = json_decode($VarJson, true);
$VarRetorno = array_column($obj['predictions'], 'description');
echo implode("<br>", $VarRetorno);
?>

If you only want the first you can change the row of the array_column to this:

if(isset($obj['predictions'][0])){
    $VarRetorno = array_column($obj['predictions'][0], 'description');
}else{
    // seu tratamento
}
  • returned it now: Notice: Array to string Conversion in C: xampp htdocs json get_json.php on line 8 Array

  • Line 8 = echo $Sweep;

  • yes careless my, as had spoken would be returned several "Description", if you want to join all just do so: echo implode("<br>", $Sweep);

0


SOLUTION FOUND:

<?php
$get = 'https://maps.googleapis.com/maps/api/place/autocomplete/json?&location=-18.866269,-41.9558537&radius=1&input=Rua%20Trin&types=address&language=pt_BR&key=dsdfdsfg21321erwr4rgf34gerg443g43hg34yh5t43tf3';

  $get = file_get_contents( $get );

  //Decodificando a string e criando o json
  $json = json_decode($get, true);
  $json = $json["predictions"];
  $get = "";

  foreach($json as $item){
  $get .= $item["description"];
  $get .= "<br>";
  }

  echo $get;
?>

RETURN:

Rua Trinta e Quatro - Lourdes, Governador Valadares - MG, Brazil

Street Trinta e Dois - Ilha dos Araújos, Governador Valadares - MG, Brazil

Rua Trinta e Oito - Ilha dos Araújos, Governador Valadares - MG, Brazil

Rua Trinta e Seis - Ilha dos Araújos, Governador Valadares - MG, Brazil

Thirty of January Street - Our Lady of Grace, Governador Valadares - MG, Brazil

  • Thank you to everyone who helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.