开发者

Array problem getting both values

I have this array and I want to get both ID and Description from it. And all I get is Array Array Array... :-)

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)
{开发者_如何学JAVA   
    echo $v.'<br />'; 
    }
}
getCountries();


Try this:

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

This is bacuse the json for item is:

"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"}]

where each item contains an id and description in an array


You're getting that output because $v is an array, and you will have to reference the specific keys of $v to see the values, eg $v['id']. You can see the keys/values of $v are in your loop by doing print_r($v); instead of echo.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜