-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');',你會怎麼做? –