2015-06-24 42 views
-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',) 

我怎样才能从中提取只是国家的名字吗?

+0

嗯,好像在'$ locArray'第九元素本身是一个数组。您似乎已经知道如何访问数组。有什么问题? –

+0

那我该如何提取国名呢? –

+0

假设你有'$ data = array('geoplugin_countryName'=>'foo');',你会怎么做? –

回答

0
$arr = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$_SERVER['REMOTE_ADDR'])); 
// then just access the country index. 
echo $arr['geoplugin_countryCode']; 

编辑:

您正在尝试echo $locArray[8];但没有指标8,因为它是关联数组(一个数组,其索引是字符串)。

0

这回像

array (
    'geoplugin_request' => '192.168.4.188', 
    'geoplugin_status' => 404, 
    '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' => '', 
    'geoplugin_dmaCode' => '', 
    'geoplugin_countryCode' => '', 
    'geoplugin_countryName' => '', 
    'geoplugin_continentCode' => '', 
    'geoplugin_latitude' => '0', 
    'geoplugin_longitude' => '0', 
    'geoplugin_regionCode' => '', 
    'geoplugin_regionName' => NULL, 
    'geoplugin_currencyCode' => NULL, 
    'geoplugin_currencySymbol' => NULL, 
    'geoplugin_currencySymbol_UTF8' => '', 
    'geoplugin_currencyConverter' => '0', 
) 

数组,然后你可以使用像

echo $locArray['geoplugin_request'];