我有这个数组,我想从中获取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();
答案 0 :(得分:2)
试试这个:
foreach($countries as $v)
{
// I am assuming $v is an array with `ID` and `Description` in it:
echo $v['id']." ".$v['description'].'<br />';
}
这是因为项目的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"}]
其中每个项目在数组中包含id
和description
答案 1 :(得分:2)
您收到该输出是因为$v
是一个数组,您必须引用$v
的特定键才能看到值,例如$v['id']
。您可以通过$v
而不是回显来查看print_r($v);
的键/值。
答案 2 :(得分:1)
echo $v['id'] . '-' . $v['description'];