简体   繁体   中英

group by item inside foreach

i need to group by "country" the results of my code, so inside i show all data by one soccer match, now i need to show sorted by country and not by id as default

 <div class="match-list-wrap">
                                                <h3 class="text-center">Tutte le partite del <?php echo $dataitaliana;?></h3>
                                                <?php                         
                                                    $campionatifix =json_decode($responsefix, true); foreach($campionatifix['response'] as $itemcampfix) { //foreach element in $arr
                                                                                        
                                                        $matchid = $itemcampfix['fixture']['id'];
                                                        $match_league = $itemcampfix['league']['country'];
                                                         $stato = $itemcampfix['fixture']['status']['long'];
                                                         $stato_minuto = $itemcampfix['fixture']['status']['elapsed'];
                                                        $match_hometeam = $itemcampfix['teams']['home']['name'];
                                                            $match_hometeamlogo = $itemcampfix['teams']['home']['logo'];
                                                            $match_hometeamgoals = $itemcampfix['goals']['home'];
                                                        $match_awayteam = $itemcampfix['teams']['away']['name'];
                                                            $match_awayteamlogo = $itemcampfix['teams']['away']['logo'];
                                                            $match_awayteamgoals = $itemcampfix['goals']['away'];?>

                                                            <div class="match-list-item">
                                                                <div class="date">
                                                                    <span><?php echo $stato;?></span>
                                                                    <?php echo $stato_minuto;?>                
                                                                </div>
                                                                <div class="logo">
                                                                    <img src="<?php echo $match_hometeamlogo;?>" class="img-polaroid">                                   
                                                                </div>
                                                                <div class="team-name">
                                                                    <?php echo $match_hometeam;?>             
                                                                </div>
                                                                <div class="team-score">
                                                                    <?php echo $match_hometeamgoals;?>                  
                                                                </div>
                                                                <div class="score-separator">:</div>
                                                                <div class="team-score">
                                                                    <?php echo $match_awayteamgoals;?>                   
                                                                </div>
                                                                <div class="team-name">
                                                                    <?php echo $match_awayteam;?>                
                                                                </div>
                                                                <div class="logo">
                                                                    <img src="<?php echo $match_awayteamlogo;?>" class="img-polaroid">              
                                                                </div>
                                                                <div class="location">
                                                                    <address>
                                                                       <?php echo $match_league;?><br><br>
                                                                    </address>
                                                                </div>
                                                                <div class="va-view-wrap">
                                                                    <a class="view-article" href="pronostico_match.php?fixtureid=<?php echo $matchid;?>">view</a>
                                                                </div>
                                                            </div>
                                                <?php } ?>
                                        </div>

this is source code:

"response": [

{

    "fixture": 

{},
"league": 
{

    "id": 200,
    "name": "Botola Pro",
    "country": "Morocco",
    "logo": "https://media.api-sports.io/football/leagues/115.png",
    "flag": "https://media.api-sports.io/flags/ma.svg",
    "season": 2019,
    "round": "Regular Season - 14"

},
"teams": 
{

    "home": 

{

    "id": 967,
    "name": "Rapide Oued ZEM",
    "logo": "https://media.api-sports.io/football/teams/967.png",
    "winner": false

},
"away": 

    {}

},
"goals": 
{},
"score": 

        {}
    }

]

so i would like print all data by country, if for example i have results with country:

  • italy
  • italy
  • england
  • france
  • england
  • italy
  • france

i need to group:

  • italy
  • italy
  • italy
  • france
  • france
  • england
  • england

try this

$out =[];

        foreach($a['response'] as $itemcampfix) 
        { 
            $out[$itemcampfix['league']['country']][] = [
                'matchid' => $itemcampfix['fixture']['id'],
                'match_league' => $itemcampfix['league']['country'],
                'stato' => $itemcampfix['fixture']['status']['long'],
                'stato_minuto' => $itemcampfix['fixture']['status']['elapsed'],
                'match_hometeam' => $itemcampfix['teams']['home']['name'],
                'match_hometeamlogo' => $itemcampfix['teams']['home']['logo'],
                'match_hometeamgoals' => $itemcampfix['goals']['home'],
                'match_awayteam' => $itemcampfix['teams']['away']['name'],
                'match_awayteamlogo' => $itemcampfix['teams']['away']['logo'],
                'match_awayteamgoals' => $itemcampfix['goals']['away']
            ];
        }
        sort($out);
        
        foreach($out as $country)
        {
            foreach ($country as $key => $item)
            {
                echo $item['match_league'].'<br/>';
                var_dump($item);
            }
            
        }

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