这是数组。
Array
(
[0] => Array
(
[position] => TMDL
[name] => Bills, Buffalo
[id] => 0251
[team] => BUF
)
[290] => Array
(
[position] => TMDL
[name] => Colts, Indianapolis
[id] => 0252
[team] => IND
)
[395] => Array
(
[position] => TMDL
[name] => Dolphins, Miami
[id] => 0253
[team] => MIA
)
[482] => Array
(
[position] => CB
[name] => Hall, Deangelo
[id] => 7398
[team] => WAS
[status] => Probable
[details] => Ankle
)
)
我要做的只是显示二维数组的内容,其中包含 [status] 和 [details] 等伤害项目,因为其中一些只有 [position] [name] [id] 和 [team] 键。下面是我到目前为止提出的代码,但它会打印数组中的所有内容。我在数组循环中尝试了array_key_exists,但我不确定我知道我在用它做什么。
$injuryData = file_get_contents('http://football.myfantasyleague.com/2013/export?TYPE=injuries&L=&W=&JSON=1&callback=');
$array1 = json_decode($injuryData, true);
$playerData = file_get_contents('http://football.myfantasyleague.com/2013/export?TYPE=players&L=&W=&JSON=1');
$array2 = json_decode($playerData, true);
function map($x) {
global $array1;
if (isset($x['id'])) {
$id = $x['id'];
$valid = array_filter($array1['injuries']['injury'], create_function('$injury', 'return $injury["id"] == "' . $id . '";'));
if (count($valid) > 0) {
$x = array_merge($x, array_shift($valid));
}
}
return $x;
}
$output = array_map('map', $array2['players']['player']);
echo "<ul>";
$result = array();
foreach ($output as $key => $category) {
if (isset($category['status'])) {
foreach ($category as $index => $value) {
$result[$index][$key] = $value;
echo "<li>" . $value . "</li>";
}
}
}
echo "</ul>";