简体   繁体   中英

How to break out of nested foreach loop and enter another foreach loop array?

I'm struggling trying to break out of this nested foreach loop.

If you take a look at my data, I'm trying to go within the team info array, find out what the team ID number is and once I know that ID number, I want to break out of the team array and go into the stats array for that particular ID(team) so I can get each statistic.

The data I'm using updates every week according to each team's rank, so the ID is the only way to know for sure the team you are looking up.

I have the following code so far and can get the team ID number but like I said I want to break out of the team array with the ID number and go into the stats array for that particular ID. Also, this data is for every NFL team and is very long, so I only posted what I could for the array so you get the idea and my foreach loop is below.

Any help is very much appreciated!!!

Array:

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 Loop:

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;
                            }
                        }
                    }
                }
            }
        }
        //}
    }
}

Here are some functions that might make your life easier. The first returns the ID value for a team based on its name or city. The second returns the stats array for team based on its ID value. All functions return false if they can't find a matching value.

// 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;
}

Example usage (based on your sample data):

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

Output:

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

Getting the stats array:

print_r(get_stats($response, 56));

Output:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM