简体   繁体   中英

how to print json array in php?

I have this json array and I want to output particular part in php.

{ "status": "success", "country": "US", "countryCode": "+1", "regionName": "REGION NAME", "city": "NY" }

example:

<?php
  echo "country: ".[country];
?>

output must be:

country: US

Use json_decode

$json = '{ 
"status": "success", 
"country": "US", 
"countryCode": "+1",   
"regionName": "REGION NAME", 
"city": "NY" 
}';
$obj = json_decode($json);
print "country: ".$obj->{'country'}; // country: US

You must convert JSON to php array:

$json = '{ "status": "success", "country": "US", "countryCode": "+1", "regionName": "REGION NAME", "city": "NY" }';
$a = json_decode($json,true);

echo "country: ".$a['country'];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM