2011-10-18 150 views
0

我有一個如下的對象數組;我如何過濾(刪除)具有admins-> member = 11的對象?如何從對象數組中過濾掉一個對象?

在這個例子中,結果數組對象只有第一個對象[245]在開始。

Array 
(
    [245] => stdClass Object 
     (
      [name] => Programmation Web 
      [description] => 
      [public] => 0 
      [jointype] => controlled 
      [grouptype] => course 
      [membershiptype] => member 
      [reason] => 
      [role] => tutor 
      [ctime] => 2011-10-12 14:41:35 
      [mtime] => 0000-00-00 00:00:00 
      [image] => 
      [session_id] => 28 
      [session_name] => Hiver 2012 
      [membercount] => 1 
      [favorite] => 
      [requests] => 0 
      [id] => 245 
      [members] => Array 
       (
        [0] => stdClass Object 
         (
          [id] => 11 
          [name] => Yves Otis (otisyves) 
         ) 

       ) 

      [projects] => Array 
       (
        [0] => stdClass Object 
         (
          [id] => 1923 
          [title] => Sans titre (1) 
          [description] => 
          [owner] => 11 
          [ownerformat] => 
          [group] => 
          [institution] => 
          [startdate] => 
          [stopdate] => 
          [ctime] => 2011-10-12 13:24:26 
          [mtime] => 2011-10-12 13:24:26 
          [atime] => 2011-10-12 13:24:26 
          [submittedgroup] => 245 
          [submittedhost] => 
          [submittedtime] => 2011-10-12 17:00:41 
          [numcolumns] => 3 
          [layout] => 
          [theme] => 
          [template] => 0 
          [copynewuser] => 0 
          [type] => portfolio 
          [visits] => 0 
          [allowcomments] => 1 
          [approvecomments] => 0 
          [skills] => 
          [instructions] => 
          [accessconf] => 
          [image] => 
          [competencesvisees] => 
          [consignes] => 
          [fichierconsignes] => 
          [groupevise] => 
         ) 

       ) 

      [project_count] => 1 
      [admins] => Array 
       (
        [0] => stdClass Object 
         (
          [member] => 11 
          [firstname] => Yves 
          [lastname] => Otis 
         ) 

       ) 

      [topic_name] => Le PHP 
      [activites] => Array 
       (
        [0] => stdClass Object 
         (
          [topic_name] => 
          [topic_id] => 42 
          [post_parent] => 107 
          [post_body] => Oui moi aussi je me demande ça. 
          [post_id] => 109 
         ) 

       ) 

      [forums] => Array 
       (
        [0] => stdClass Object 
         (
          [forum_name] => Discussion générale 
          [forum_id] => 101 
         ) 

       ) 

     ) 

    [246] => stdClass Object 
     (
      [name] => Les bases de données 
      [description] => 
      [public] => 0 
      [jointype] => controlled 
      [grouptype] => course 
      [membershiptype] => admin 
      [reason] => 
      [role] => admin 
      [ctime] => 2011-10-13 15:27:30 
      [mtime] => 0000-00-00 00:00:00 
      [image] => 
      [session_id] => 27 
      [session_name] => Automne 2011 
      [membercount] => 0 
      [favorite] => 
      [requests] => 0 
      [id] => 246 
      [project_count] => 0 
      [topic_name] => Difficulté 
      [activites] => Array 
       (
        [0] => stdClass Object 
         (
          [topic_name] => 
          [topic_id] => 44 
          [post_parent] => 111 
          [post_body] => Ouah! 
          [post_id] => 112 
         ) 

       ) 

      [forums] => Array 
       (
        [0] => stdClass Object 
         (
          [forum_name] => Le MySQL 
          [forum_id] => 103 
         ) 

       ) 

     ) 

) 

我想檢查對指數

+0

重複http://stackoverflow.com/questions/7826358/how-to -filter-an-array-of-object – SparrowG

回答

2
$arr = array_filter($arr, function($item) 
{ 
    if(!isset($item->admins) || !is_array($item->admins)) 
    { 
     return false; 
    } 
    foreach($item->admins as $admin) 
    { 
     if($admin->member == 11) 
     { 
      return true; 
     } 
    } 
    return false; 
}); 

(PHP 5.3中使用匿名函數)

+0

可能還想使用if(property_exists($ item,'admins'))來確保對象實際上有一個管理變量。這個問題並不清楚他的對象是否是同質的 – carpii

+0

嗨carpii,是的的確我們應該檢查對象是否是同質的第一... 蒂姆,我怎麼能過濾使用for循環的上述數組?因爲它看起來像明確使用索引0的答案,因爲在我的例子中,這是出現$ item-> admins [0] - > member == 11的地方,但是如果我不知道哪個對象有什麼? 謝謝! – Alex

+0

@Alex:查看我剛纔編輯的內容是否解決了您提到的問題。 –