如何询问对象内数组中的某些对象

时间:2017-07-14 09:39:45

标签: php mysql arrays string

我遇到了这个问题。我试图从对象中获取数组中的某些对象。

我做了这个功能,我要求显示一个匹配。

function match () {

    $Match = WaterpoloAPI::call("Matches", "getMatch", Array($_GET["MatchId"]));
    echo "<td>$Match->Date</td> <td>$Match->Time</td> <td>$Match->PoolName</td><td class='text-center'>$Match->HomeTeam </td><td><strong><a href='wedstrijd?MatchId=".$Match->Id."'>$Match->ResultHome - $Match->ResultGuest </a></strong></td><td> $Match->AwayTeam</td>";
}

我可以从该匹配中选择一个项目列表来显示...

MatchItem

Properties

Name    Type
Id  string
MatchNumber int
Date    string
Time    string
DepartmentId    string
DepartmentCode  string
DepartmentName  string
HomeTeamId  string
HomeTeam    string
AwayTeamId  string
AwayTeam    string
PoolId  string
PoolName    string
PoolCity    string
MatchReport string
Played  boolean
ResultHome  int
ResultGuest int
**Referees  MatchRefereeItem[]**

但是我想展示裁判...但是它在阵列中...我该怎么做?

**MatchRefereeItem
Properties**

Name    Type
Id  string
Initials    string
FirstName   string
Insertion   string
LastName    string
Sex string
Indication  int

我还在学习,也许这是一个愚蠢的问题,所以我很抱歉。但如果有人可以帮助我,那就太棒了。

2 个答案:

答案 0 :(得分:0)

MatchItem类应该有一个像getRefereeItems这样的方法来返回裁判员数组。或者属性是公开的。

然后你可以做这样的事情:

$referees = [];
foreach($match->Referees as $refereeItem) {
    $referees[] = $refereeItem->FirstName . ' ' . $refereeItem->LastName;
}

echo implode(', ', $referees);

答案 1 :(得分:0)

Fabian当我做var_dump($Match)时,它会向我显示每个对象的数据

object(stdClass)#4911 (19) {
 ["Id"]=> string(15) "WW0000000001837" 
 ["MatchNumber"]=> int(890)
 ["Date"]=> string(10) "16-12-2016"
 ["Time"]=> string(5) "20:30" 
 ["DepartmentId"]=> string(15) "WW0000000000085"
 ["DepartmentCode"]=> string(5) "BC SD"
 ["DepartmentName"]=> string(17) "Beker/Coupe Dames"
 ["HomeTeamId"]=> string(15) "WW0000000000574"
 ["HomeTeam"]=> string(9) "Eeklo MZV"
 ["AwayTeamId"]=> string(15) "WW0000000000570" 
 ["AwayTeam"]=> string(17) "Leuven Aqua LAQUA"
 ["PoolId"]=> string(15) "WW0000000000024" 
 ["PoolName"]=> string(18) "Stedelijk Zwembad "
 ["PoolCity"]=> string(5) "Eeklo" 
 ["MatchReport"]=> string(2) "NO" 
 ["Played"]=> bool(true) 
 ["ResultHome"]=> int(18) 
 ["ResultGuest"]=> int(4)
 ["Referees"]=> array(2) {
    [0]=> object(stdClass)#4907 (7) {
            ["Id"]=> string(15) "WW0000000000052"
            ["Initials"]=> string(0) "" 
            ["FirstName"]=> string(7) "Niculae" 
            ["Insertion"]=> string(0) "" 
            ["LastName"]=> string(8) "Fulgeanu"
            ["Sex"]=> string(1) "M"
            ["Indication"]=> int(1)
        } 
        [1]=> object(stdClass)#4865 (7) { 
            ["Id"]=> string(15) "WW0000000000054"
            ["Initials"]=> string(0) "" 
            ["FirstName"]=> string(6) "Wouter" 
            ["Insertion"]=> string(0) ""
            ["LastName"]=> string(8) "Fontaine"
            ["Sex"]=> string(1) "M" 
            ["Indication"]=> int(2) } 
        } 
 }