這是數組。僅顯示包含Multidimentional數組中的某些鍵的項目
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
)
)
我所試圖做的是隻顯示二維數組有傷害的項目,如[狀態]和[詳細信息]的內容,因爲他們中的一些僅具有[位置] [名] [ID ]和[團隊]鍵。下面是我迄今爲止編寫的代碼,但是它打印了數組中的所有內容。我在數組循環中嘗試了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>";
我打算使用array_key_exists,但據推測isset()速度更快,但是誰知道...... –