2017-07-14 59 views
1

我得到了这个问题。我试图从一个对象中获取数组中的某些对象。如何问一个对象内的某个对象内的某个对象

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

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 

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

+1

尝试https://stackoverflow.com/questions/ 4414623/loop-through-an-array -php –

+0

u_mulder thx为你的答案,但它仍然无法正常工作......我试过这个foreach($ Referees as $ Referee){ \t echo“$ Referee-> FirstName”; \t} –

回答

0

MatchItem类应该有一个方法,如​​返回引用数组。或者这些属性是公开的。

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

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

echo implode(', ', $referees); 
+0

Thx Fabian,所以我尝试了你的代码,像这样$ Referees = []; foreach($ Match-> getReferees()as $ RefereeItem){ $ Referees [] = $ RefereeItem-> FirstName(); \t echo implode(',',$ Referees); }仍然不工作....我做错了什么? –

+0

有人可以帮我吗? thx –

+0

你会收到任何错误信息吗?你有没有检查你的错误报告,其设置为E_ALL?否则,尝试var_dump($ Match);并张贴我这些裁判的真实结构,这样我可以帮助更好。 –

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) } 
     } 
} 
+0

好吧,你可以访问属性为公共。看我编辑我的代码示例https://stackoverflow.com/questions/45099649/how-to-ask-certain-objects-within-a-array-within-an-object?answertab=active#tab-top –

+1

Thx Fabian你做了我的一天......它的工作......我很高兴:D –

相关问题