2012-09-03 76 views
1

希望这个称号是有道理的,但我的问题是,我有一个对象,像这样的阵列的子阵列上声明,这只是1从阵列为例如果对象

object(stdClass)#6 (3) { 
    ["items"]=> 
    array(40) { 
    [0]=> 
    object(stdClass)#7 (22) { 
     ["id"]=> 
     int(46) 
     ["parentId"]=> 
     int(0) 
     ["name"]=> 
     string(22) "Complete monthly wages" 
     ["description"]=> 
     string(294) "Complete monthly wages<span style=""></span><br /><div>Complete monthly wages<span style=""></span><br /></div><div>Complete monthly wages<span style=""></span><br /></div><div>Complete monthly wages<span style=""></span><br /></div><div>Complete monthly wages<span style=""></span><br /></div>" 
     ["tags"]=> 
     string(0) "" 
     ["projectId"]=> 
     int(12) 
     ["ownerId"]=> 
     int(1) 
     ["groupId"]=> 
     int(0) 
     ["startDate"]=> 
     string(19) "2012-09-03T00:00:00" 
     ["priority"]=> 
     int(2) 
     ["progress"]=> 
     float(0) 
     ["status"]=> 
     int(10) 
     ["createdAt"]=> 
     string(19) "2012-09-03T07:35:21" 
     ["updatedAt"]=> 
     string(19) "2012-09-03T07:35:21" 
     ["notifyProjectTeam"]=> 
     bool(false) 
     ["notifyTaskTeam"]=> 
     bool(false) 
     ["notifyClient"]=> 
     bool(false) 
     ["hidden"]=> 
     bool(false) 
     ["flag"]=> 
     int(0) 
     ["hoursDone"]=> 
     float(0) 
     ["estimatedTime"]=> 
     float(0) 
     ["team"]=> 
     object(stdClass)#8 (3) { 
     ["items"]=> 
     array(2) { 
      [0]=> 
      object(stdClass)#9 (1) { 
      ["id"]=> 
      int(2) 
      } 
      [1]=> 
      object(stdClass)#10 (1) { 
      ["id"]=> 
      int(1) 
      } 
     } 
     ["count"]=> 
     int(2) 
     ["total"]=> 
     int(2) 
     } 
    } 

我们可以看到它有一个团队部分,这是我的重点

["team"]=> 
      object(stdClass)#8 (3) { 
      ["items"]=> 
      array(2) { 
       [0]=> 
       object(stdClass)#9 (1) { 
       ["id"]=> 
       int(2) 
       } 
       [1]=> 
       object(stdClass)#10 (1) { 
       ["id"]=> 
       int(1) 
       } 
      } 
      ["count"]=> 
      int(2) 
      ["total"]=> 
      int(2) 
      } 
     } 

正如你可以看到有2点在那里,1和2,有可能是不超过30左右,但我无法弄清楚如何高效地告诉它搜索整个阵列。

如果我使用它,只要id 1恰好是id中的第一项,但这种情况显然并非总是如此。我的目标是通过对象进行搜索,只是运行代码,如果用户ID是球队阵中,即时通讯新的PHP和特别的对象,以便希望有人能指出我在正确的方向

foreach($tasksList->items as $task_details) 
{ 
    if($task_details->team->items->id === 1) 
    { 
     echo "My Code"; 

    } 


} 

回答

0

您的代码(和上面粘贴的[team]部分)似乎错过了team->items数组这一事实。在顶部示例中,它看起来像:

["team"]=> 
    object(stdClass)#8 (3) { 
    ["items"]=> 
    /* It's an array! */ 
    array(2) { 
    ... 
    } 
    } 

它不会直接具有id属性。相反,您需要迭代team->items

foreach ($taskList->items as $task_details) { 
    foreach ($task_details->team->items as $key => $value) { 
    echo "Team item #: $key ... Team id: $value->id\n"; 
    if ($value->id === 1) { 
     echo "Matched Team ID 1\n"; 
    } 
    } 
}