doujiao3998 2017-07-24 12:07
浏览 24
已采纳

在两个单独的主题foreach过滤对象if

I have a question. I am trying to filter a match game. In the match game I want to filter the players ...but because there is a home and away team ....I want to seperate them. How is this possible.

my structure

Name    Type
Id  string
MatchNumber int
Date    string
Time    string
ClockDirection  string
TrueStartTime   string
TrueEndTime string
DepartmentId    string
DepartmentCode  string
DepartmentName  string
HomeTeamId  string
HomeClubId  string
HomeTeam    string
AwayTeamId  string
AwayClubId  string
AwayTeam    string
PoolId  string
PoolName    string
ResultHome  int
ResultGuest int
PeriodStandings MatchReportPeriodStandingsItem[]
Actions MatchReportActionsItem[]
Players MatchReportPlayersItem[]
Coaches MatchReportCoachesItem[]
Referees    MatchReportRefereesItem[]
Officials   MatchReportOfficialsItem[]

for the players info it's like this

MatchReportPlayersItem

Properties

Name    Type
Id  string
Cap string
Name    string
Captain boolean
Team    string

If I want to show a list of players It's like this

$Players = [];
                    foreach($Match->Players as $playerItem) {
                    $Players[] = $playerItem->Name . ' ' . $playerItem->Team;
                }

resulting in

De Craemer H, Van Der Linden H, Baeckelandt H, Christiaens H, Degryse H, Devos H, Dumoulin H, Gevaert H, Gheysen H, Louage H, Mestdag H, Monballieu H, Peremans H, Stieperaere H, Van Den Berghe H, Van Hoestenberghe H, Verschelde H, Verschelde H, Bekar G, Benabdelouahed G, Bochet G, Bourgeois G, Brahim G, Brismee G, Brismee G, Broutard G, Carin G

where the H and G ...stands for Home and Guest ....I want it in two separate list/table ...how am I gonna accomplish this.

So far I got this.

$Players = [];
                    foreach($Match->Players as $playerItem) {
                    $Players[] = $playerItem->Name . ' ' . $playerItem->Team;
                } 
         if ($playerItem->Team == "G"){
        echo implode(', ', $Players); } 
        else {
    echo " ";
}

can someone help me on this subject thanx!

  • 写回答

1条回答 默认 最新

  • dqmg80654 2017-07-24 12:27
    关注

    You can do like this:

    <?php
    
    class Match {
    
        public $Players;
    
        public function __construct($players) {
            $this->Players = $players;
        }
    
    }
    
    class Player {
    
        public $Name;
        public $Team;
    
        public function __construct($name, $team) {
            $this->Name = $name;
            $this->Team = $team;
        }
    
    }
    
    $Match = new Match(
            array(
                new Player("John", "Gremio"),
                new Player("Mike", "Gremio"),
                new Player("Tom", "Inter"),
                new Player("Silva", "Inter")
            )
        );
    
    $Players = [];
    foreach ($Match->Players as $playerItem) {
        $Players[$playerItem->Team][] = $playerItem->Name;
    }
    
    var_dump($Players);
    

    This output:

    array(2) {
      'Gremio' =>
      array(2) {
        [0] =>
        string(4) "John"
        [1] =>
        string(4) "Mike"
      }
      'Inter' =>
      array(2) {
        [0] =>
        string(3) "Tom"
        [1] =>
        string(5) "Silva"
      }
    }
    

    EDITED (new solution):

    <?php
    
    class Match {
    
        public $Players;
    
        public function __construct($players) {
            $this->Players = $players;
        }
    
    }
    
    class Player {
    
        public $Name;
        public $Team;
    
        public function __construct($name, $team) {
            $this->Name = $name;
            $this->Team = $team;
        }
    
    }
    
    function getPlayersFromTeam($team, $players) {
        $Players = array();
        foreach ($players as $playerItem) {
            if ($playerItem->Team == $team) {
                $Players[$playerItem->Team][] = $playerItem->Name;
            }
        }
        return $Players;
    }
    
    $Match = new Match(
            array(
        new Player("John", "Gremio"),
        new Player("Mike", "Gremio"),
        new Player("Tom", "Inter"),
        new Player("Silva", "Inter")
            )
    );
    
    var_dump(getPlayersFromTeam("Gremio", $Match->Players));
    var_dump(getPlayersFromTeam("Inter", $Match->Players));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办