php stdClass访问

php stdClass访问,php,arrays,object,stdclass,Php,Arrays,Object,Stdclass,我有这样一个输出: stdClass Object ( [GetMatchdataByLeagueDateTimeResult] => stdClass Object ( [Matchdata] => Array ( [0] => stdClass Object (

我有这样一个输出:

stdClass Object
(
    [GetMatchdataByLeagueDateTimeResult] => stdClass Object
        (
            [Matchdata] => Array
                (
                    [0] => stdClass Object
                        (
                            [teamId] => 40
在foreach循环中

foreach ($allMatches as $match):
我现在想处理如下数据:

if ($match->idTeam1 == $teamId || $match->idTeam2 == $teamId):
但我得到了这个错误:

正在尝试获取非对象的属性

原因是,Matchdata数组包含大约60多个条目,我想过滤掉其中
[idTeam1]
[idTeam2]
==给定id的条目

因此,我应该只有大约5至7个条目

在使用stdClass对象时,最好的方法是什么

请帮忙


谢谢

假设
$response
是您发布的结构:

$teamId = 99; // target teamId

foreach($response->GetMatchdataByLeagueDateTimeResult->MatchData as $match)
{
    if ($match->idTeam1 == $teamId || $match->idTeam2 == $teamId)
    {
        // this `$match` has the target $teamId - do something
    }
}
好吧,我只是个瞎子: foreach($allMatches->GetMatchDataByleAgueDataTimeResult->Matchdata as$id=>$match): 调试($id); 调试($match->idTeam1)

现在它可以工作了:如果($match->idTeam1==$teamId | |$match->idTeam2==$teamId)


无论如何,非常感谢

您的外部结构是一个对象,因此您可能正在迭代对象的可见属性(请参见本页)。如果要在对象实例中遍历数组,应该将其传递给foreach循环,或者让类实现迭代器接口并将其方法重定向到数组

 <?php
       foreach ($object->allMatches as $match) {
              /* Your iterate body here */
       }

在哪里声明了
idTeam1
idTeam2
?您的对象结构是什么?您的输出示例(假设为$allMatches?)与您的伪代码不匹配。teamId vs idTeam1-idTeam1来自哪里?idTeam1是否有效?不是100%确定,但似乎需要使用
gettype()
PHP函数测试变量类型:…使用对象与关联数组是否有原因?