2011-10-19 16 views
13

Ho una matrice di oggetti (mostra sotto) e vorrei scrivere una funzione che restituisce lo stesso array ma con "oggetto (i)" che soddisfa il criterio rimosso.Come filtrare un array di oggetti?

La funzione sarebbe:

1- controllo se l'indice esiste 2- se esiste, i controlli per il valore desiderato e se indice dell'oggetto è uguale a tale valore, rimuovere l'intero oggetto.

Ad esempio:

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 
           ) 

         ) 

       ) 

     ) 

) 

Se c'è un oggetto la cui admins-> membro valore è uguale a 11, rimuovere l'oggetto e restituire la matrice senza questo oggetto. L'array restituito sarebbe quindi:

 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 
           ) 

         ) 

       ) 

     ) 

) 

Come farei per farlo?

+0

Dai un'occhiata a ['array_filter'] (http://php.net/array_filter). –

+0

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

risposta

29

desidera filtrare un array? Utilizza 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

Ciao! Grazie per la tua risposta pulita! Sembra che funzioni, ma non funziona ... $ new_array è lo stesso di $ array dopo che l'ho passato attraverso la funzione di callback. – Alex

+0

Davvero molto strano, ha funzionato per me. Osservando la struttura dei dati, non riesco a capire perché non ...: S – netcoder

+0

L'array inizia con un indice "corso", sarebbe per questo che non funziona? – Alex

3

È possibile utilizzare array_filter con un callback personalizzato:

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

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