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

时间:2017-07-24 12:07:20

标签: php arrays if-statement foreach

我有一个问题。我正在尝试过滤比赛游戏。 在比赛中我想过滤球员......但是因为有一个主队和客场球队....我想分开他们。这怎么可能。

我的结构

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[]

对于玩家信息,就像这样

MatchReportPlayersItem

Properties

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

如果我想显示一个玩家列表就像这样

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

导致

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

其中H和G代表Home和Guest ....我想把它放在两个单独的列表/表中......我将如何实现这一目标。

到目前为止,我得到了这个。

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

有人可以帮助我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

你可以这样做:

<?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);

此输出:

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(新解决方案):

<?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));