【问题标题】:How to ask certain objects within a array within an object如何在对象内的数组中询问某些对象
【发布时间】:2017-12-19 08:36:58
【问题描述】:

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

我创建了这个功能,要求显示单个匹配项。

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

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

【问题讨论】:

标签: php mysql arrays string


【解决方案1】:

MatchItem 类应该有一个类似getRefereeItems 的方法,它返回裁判数组。或者属性是公开的。

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

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

echo implode(', ', $referees);

【讨论】:

  • 感谢 Fabian,所以我尝试了您的代码,例如 $Referees = []; foreach($Match->getReferees() as $RefereeItem) { $Referees[] = $RefereeItem->FirstName();回声内爆(',',$Referees);仍然无法正常工作....我做错了什么?
  • 您是否收到任何错误消息?您是否检查了错误报告,它设置为 E_ALL?否则尝试 var_dump($Match);并将这些裁判的真实结构发布给我,以便我可以提供更好的帮助。
  • 它没有显示任何内容...结构可以在test.waterpolo-online.com/service 看到..选项匹配 ....我想显示与比赛相关的裁判
  • 我需要更多信息。财产是私有的还是公共的? $match 有哪些方法?参见示例3v4l.org/RtOjS
【解决方案2】:

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

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2018-01-25
  • 2018-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-22
  • 2013-12-18
相关资源
最近更新 更多