如何打破嵌套的foreach循环并输入另一个foreach循环数组?

时间:2019-12-14 10:18:37

标签: php multidimensional-array foreach

我正在努力摆脱这个嵌套的foreach循环。

如果您查看我的数据,我将尝试进入团队信息数组,找出团队ID号是什么,一旦知道该ID号,我便想脱离团队数组,进入该特定ID(团队)的统计信息数组,这样我就可以获取每个统计信息。

我使用的数据每周都会根据每个团队的排名进行更新,因此ID是唯一确定您要查找的团队的方式。

到目前为止,我具有以下代码,并且可以获取团队ID号,但是就像我说的那样,我想突破带有ID号的team数组,并进入该特定ID的stats数组。而且,此数据适用于每个NFL团队,而且非常长,因此我只发布了我可以为阵列提供的数据,因此您了解了这一点,下面是我的foreach循环。

非常感谢您的帮助!

数组:

Array(
[overallteamstandings] => Array
    (
        [lastUpdatedOn] => 2019-12-13 4:03:05 AM
        [teamstandingsentry] => Array
            (
                [0] => Array
                    (
                        [team] => Array
                            (
                                [ID] => 56
                                [City] => Baltimore
                                [Name] => Ravens
                                [Abbreviation] => BAL
                            )

                        [rank] => 1
                        [stats] => Array
                            (
                                [GamesPlayed] => Array
                                    (
                                        [@abbreviation] => G
                                        [#text] => 14
                                    )

                                [PassAttempts] => Array
                                    (
                                        [@category] => Passing
                                        [@abbreviation] => Att
                                        [#text] => 384
                                    )

                                [PassCompletions] => Array
                                    (
                                        [@category] => Passing
                                        [@abbreviation] => Comp
                                        [#text] => 255
                                    )

                                [PassPct] => Array
                                    (
                                        [@category] => Passing
                                        [@abbreviation] => Pct
                                        [#text] => 66.4
                                    )

                                [PassGrossYards] => Array
                                    (
                                        [@category] => Passing
                                        [@abbreviation] => Yds
                                        [#text] => 3016
                                    )

Foreach循环:

foreach ($response as $overallteamstandings => $b) {
    foreach ($b['teamstandingsentry'] as $key => $d) {
        //if ($key == '9'){
        foreach ($d as $cat => $info) {
            if ($cat == 'team') {
                foreach ($info as $c => $v) {
                    if ($c == 'ID') {
                        echo $v;

                        if ($v == '59') {
                            //break 4;
                        }
                    }
                }
            }

            if ($cat == 'stats') {
                foreach ($info as $category => $stats) {
                    if ($category == 'Wins') {
                        foreach ($stats as $val => $value) {
                            if ($val == '#text') {
                                echo $value . "-";
                            }
                        }
                    }
                    if ($category == 'Losses') {
                        foreach ($stats as $val => $value) {
                            if ($val == '#text') {
                                echo $value;
                            }
                        }
                    }
                }
            }
        }
        //}
    }
}

1 个答案:

答案 0 :(得分:0)

有些功能可以使您的生活更轻松。第一个根据团队的名称或城市返回ID值。第二个基于其stats值返回团队的ID数组。如果所有函数都找不到匹配的值,则会返回false

// get a team's id based on its name or city
function get_team_id($response, $team, $type) {
    $teams = array_column($response['overallteamstandings']['teamstandingsentry'], 'team');
    switch ($type) {
        case 'name':
            $key = array_search($team, array_column($teams, 'Name'));
            return ($key !== false) ? $teams[$key]['ID'] : false;
            break;
        case 'city':
            $key = array_search($team, array_column($teams, 'City'));
            return ($key !== false) ? $teams[$key]['ID'] : false;
            break;
        default:
            return false;
    }
}

// get a team's stats based on its ID
function get_stats($response, $team) {
    $teams = array_column($response['overallteamstandings']['teamstandingsentry'], 'team');
    $key = array_search($team, array_column($teams, 'ID'));
    return ($key !== false) ? $response['overallteamstandings']['teamstandingsentry'][$key]['stats'] : false;
}

用法示例(基于您的示例数据):

echo "ID for city Baltimore is " . get_team_id($response, 'Baltimore', 'city') . "\n";
echo "ID for name Ravens is " . get_team_id($response, 'Ravens', 'name') . "\n";

输出:

ID for city Baltimore is 56
ID for name Ravens is 56

获取统计信息数组:

print_r(get_stats($response, 56));

输出:

Array
(
    [GamesPlayed] => Array
        (
            [@abbreviation] => G
            [#text] => 14
        )
    [PassAttempts] => Array
        (
            [@category] => Passing
            [@abbreviation] => Att
            [#text] => 384
        )
    [PassCompletions] => Array
        (
            [@category] => Passing
            [@abbreviation] => Comp
            [#text] => 255
        )
    [PassPct] => Array
        (
            [@category] => Passing
            [@abbreviation] => Pct
            [#text] => 66.4
        )
    [PassGrossYards] => Array
        (
            [@category] => Passing
            [@abbreviation] => Yds
            [#text] => 3016
        )
)

Demo on 3v4l.org