0

这是数组。

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>";
4

2 回答 2

0

在 $category 上添加搜索以仅显示具有详细信息的数组项(如有关伤害的详细信息):

    if (array_key_exists("details", $category)) {
      foreach( $category as $index => $value ) {
        $result[$index][$key]= $value;
        echo "<li>" . $value . " </li>" ;
      } 
    }
于 2013-08-29T02:34:35.313 回答
0

如果我没看错,在你的地图功能中你可以这样做:

if(!array_key_exists('status', $x))
    return;

如果输入“$x”没有“状态”键,它会在执行任何操作之前返回。

于 2013-08-29T01:59:06.153 回答