【问题标题】:Only show items containing certain keys in Multidimentional array仅显示包含多维数组中某些键的项目
【发布时间】:2013-08-29 14:45:43
【问题描述】:

这是数组。

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

【问题讨论】:

    标签: php multidimensional-array array-key-exists


    【解决方案1】:

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

        if (array_key_exists("details", $category)) {
          foreach( $category as $index => $value ) {
            $result[$index][$key]= $value;
            echo "<li>" . $value . " </li>" ;
          } 
        }
    

    【讨论】:

    • 我打算使用 array_key_exists 但据说 isset() 更快,但谁知道...
    【解决方案2】:

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

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

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

    【讨论】:

    • 其实我在你发帖之前就想通了,看看我的编辑。
    • 谢谢,今晚这里的人似乎不多。
    猜你喜欢
    • 2018-09-06
    • 1970-01-01
    • 1970-01-01
    • 2011-07-12
    • 1970-01-01
    • 2014-09-01
    • 1970-01-01
    • 2014-04-27
    • 1970-01-01
    相关资源
    最近更新 更多