这是阵列。
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中的所有内容。我在数组循环中尝试了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>";
答案 0 :(得分:0)
如果我正确阅读,你可以在地图功能中执行此操作:
if(!array_key_exists('status', $x))
return;
如果输入'$ x'没有'status'键,那么在做任何事情之前会返回。
答案 1 :(得分:0)
在$ category上添加搜索以仅显示包含详细信息的数组项(如损伤的详细信息):
if (array_key_exists("details", $category)) {
foreach( $category as $index => $value ) {
$result[$index][$key]= $value;
echo "<li>" . $value . " </li>" ;
}
}