2011-10-19 93 views
13

我有一個對象數組(下面顯示),我想寫一個函數返回相同的數組,但與符合條件刪除的「對象」。如何篩選對象數組?

則該函數將:

1-檢查如果索引存在 2-如果存在,爲所需的值,並且如果該對象的索引等於該值的檢查,移除整個對象。

例如:

Array 
(
    [course] => Array 
     (
      [0] => stdClass Object 
       (
        [name] => Programmation Web 
        [description] => 
        [public] => 0 
        [requests] => 0 
        [id] => 245 
        [members] => Array 
         (
          [0] => stdClass Object 
           (
            [id] => 11 
            [name] => Robert Smith 
           ) 

         ) 

        [projects] => Array 
         (
          [0] => stdClass Object 
           (
            [id] => 1923 
            [title] => Sans titre (1) 
            [type] => portfolio 
           ) 

          ) 

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

         ) 

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

         ) 

       ) 

      [1] => stdClass Object 
       (
        [name] => Les bases de données 
        [description] => 
        [public] => 0 
        [jointype] => controlled 
        [grouptype] => course 
        [membershiptype] => admin 
        [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 
           ) 

         ) 

       ) 

     ) 

) 

如果有一個對象,其admins->構件值等於11,刪除該對象沒有這個對象返回數組。返回的陣列因此將是:

 Array 
(
    [course] => Array 
     (
      [0] => stdClass Object 
       (
        [name] => Programmation Web 
        [description] => 
        [public] => 0 
        [requests] => 0 
        [id] => 245 
        [members] => Array 
         (
          [0] => stdClass Object 
           (
            [id] => 11 
            [name] => Robert Smith (smithrobert) 
           ) 

         ) 

        [projects] => Array 
         (
          [0] => stdClass Object 
           (
            [id] => 1923 
            [title] => Sans titre (1) 
            [type] => portfolio 
           ) 

          ) 

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

         ) 

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

         ) 

       ) 

     ) 

) 

我該怎麼做呢?

+0

看看['array_filter'](http://php.net/array_filter)。 –

+0

Duplicate(http://stackoverflow.com/questions/7814872/how-to-filter-out-an-object-from-an-array-of-objects) – SparrowG

回答

29

要過濾的陣列?使用array_filter

$new_array = array_filter($array, function($obj){ 
    if (isset($obj->admins)) { 
     foreach ($obj->admins as $admin) { 
      if ($admin->member == 11) return false; 
     } 
    } 
    return true; 
}); 
+0

嗨!感謝您的簡潔答案!它看起來應該可以工作,但它不工作... $ new_array和我通過回調函數傳遞之後的$ array一樣。 – Alex

+0

確實很奇怪,它爲我工作。通過查看您的數據結構,我不明白爲什麼它不會......:S – netcoder

+0

該數組以索引「course」開頭,這是它爲什麼不起作用的原因嗎? – Alex

3

可以使用array_filter使用自定義的回調:

function filter_callback($element) { 
    if (isset($element->foo) && $element->foo == 'some_value') { 
    return TRUE; 
    } 
    return FALSE; 
} 

$arr = array_filter($arr, 'filter_callback');