如何从字符串表示的数组中获取特定信息?

时间:2014-02-15 09:42:17

标签: php json

当我把这个http://api.hostip.info/get_json.php放在url栏中时,它给我一个这种格式的数组如何从中提取信息?

{"country_name":"PAKISTAN","country_code":"PK","city":"Lahore","ip":"180.178.147.131"}

1 个答案:

答案 0 :(得分:0)

在PHP中使用json_decode()

<?php
$jsonstringfromyourapi='{"country_name":"PAKISTAN","country_code":"PK","city":"Lahore","ip":"180.178.147.131"}';
$arr=json_decode($jsonstringfromyourapi,1);

foreach($arr as $k=>$v)
{
echo "$k = $v<br>";
}

Demo