I've got an array of objects (show below) and I would like to write a function that returns the same array but with the "object(s)" that meet the criterion removed.
The function would :
1- check if the index exists 2- if it exists, checks for the required value and if the object's index is equal to that value, remove the whole object.
For example :
    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                                 )                          )                  )          )  )   If there's an object whose admins->member value is equal to 11, remove the object and return the array without this object. The returned array would thus be :
        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                                 )                          )                  )          )  )   How would I go about doing that?
JavaScript Array filter()The filter() method creates a new array filled with elements that pass a test provided by a function. The filter() method does not execute the function for empty elements. The filter() method does not change the original array.
To filter an array of objects in React:Call the filter() method on the array. On each iteration, check if a certain condition is met. The Array. filter methods returns an array with all elements that satisfy the condition.
Want to filter an array? Use 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; }); 
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With