-1
我正在使用geoplugin来了解用户的国家/地区。在网站上,他们写下面的代码:无法在PHP中返回数组中元素的值
echo var_export(unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$_SERVER['REMOTE_ADDR'])));
,其结果是:
array (
'geoplugin_request' => '59.179.68.40',
'geoplugin_status' => 206,
'geoplugin_credit' => 'Some of the returned data includes GeoLite data created by MaxMind, available from http://www.maxmind.com.',
'geoplugin_city' => '',
'geoplugin_region' => '',
'geoplugin_areaCode' => '0',
'geoplugin_dmaCode' => '0',
'geoplugin_countryCode' => 'IN',
'geoplugin_countryName' => 'India',
'geoplugin_continentCode' => 'AS',
'geoplugin_latitude' => '20',
'geoplugin_longitude' => '77',
'geoplugin_regionCode' => '',
'geoplugin_regionName' => NULL,
'geoplugin_currencyCode' => 'INR',
'geoplugin_currencySymbol' => '₨',
'geoplugin_currencySymbol_UTF8' => '₨',
'geoplugin_currencyConverter' => '63.4999',
)
现在,我试图将其存储在一个这样的数组:
$locArray = var_export(unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$_SERVER['REMOTE_ADDR'])));
和呼应位置如:
echo $locArray[8];
In呼应第九元素,这样的代码回声完整阵列代替:
array ('geoplugin_request' => '59.179.68.40', 'geoplugin_status' => 206, 'geoplugin_credit' => 'Some of the returned data includes GeoLite data created by MaxMind, available from http://www.maxmind.com.', 'geoplugin_city' => '', 'geoplugin_region' => '', 'geoplugin_areaCode' => '0', 'geoplugin_dmaCode' => '0', 'geoplugin_countryCode' => 'IN', 'geoplugin_countryName' => 'India', 'geoplugin_continentCode' => 'AS', 'geoplugin_latitude' => '20', 'geoplugin_longitude' => '77', 'geoplugin_regionCode' => '', 'geoplugin_regionName' => NULL, 'geoplugin_currencyCode' => 'INR', 'geoplugin_currencySymbol' => '₨', 'geoplugin_currencySymbol_UTF8' => '₨', 'geoplugin_currencyConverter' => '63.4999',)
我怎样才能从中提取只是国家的名字吗?
嗯,好像在'$ locArray'第九元素本身是一个数组。您似乎已经知道如何访问数组。有什么问题? –
那我该如何提取国名呢? –
假设你有'$ data = array('geoplugin_countryName'=>'foo');',你会怎么做? –