2011-05-23 75 views
0

我有這個數組,我想從中獲取ID和說明。而我得到的是陣列陣列陣列... :-)獲取兩個值的數組問題

function getCountries() { 
$json = file_get_contents('http://onleague.stormrise.pt:8031/OnLeagueRest/resources/onleague/Utils/Countries'); 
$data = json_decode($json, TRUE); 
$countries = array(); 
foreach($data['data']['item'] as $item) { 
    $countries[] = $item; 
} 
//print_r($countries); 
foreach($countries as $v) 
{ 
    echo $v.'<br />'; 
    } 
} 
getCountries(); 
+1

'print_r($ countries)'是什麼結果? – Neal 2011-05-23 19:42:43

回答

2

試試這個:

foreach($countries as $v) 
{ 
    // I am assuming $v is an array with `ID` and `Description` in it: 
    echo $v['id']." ".$v['description'].'<br />'; 
} 

這是bacuse的JSON的產品:

"item": 
    [{"id":"DE","description":"Deutschland"}, 
    {"id":"ES","description":"España"}, 
    {"id":"FR","description":"France"}, 
    {"id":"PT","description":"Portugal"}, 
    {"id":"UK","description":"United Kingdom"}, 
    {"id":"US","description":"United States"}] 

,其中每個項目包含陣列中的iddescription

2

您正在獲取該輸出,因爲$v是一個數組,您將不得不引用$v的特定鍵以查看值,例如$v['id']。通過執行print_r($v);而不是回顯,您可以看到$v的鍵/值在您的循環中。

1
echo $v['id'] . '-' . $v['description'];