2015-06-24 63 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'];