Tengo una matriz de objetos (ver a continuación) y me gustaría escribir una función que devuelva la misma matriz pero con el "objeto (s)" que cumplan con el criterio eliminado.¿Cómo se filtra una matriz de objetos?
La función sería:
1- verificación si existe el índice 2- si existe, comprueba el valor requerido y si el índice del objeto es igual a ese valor, eliminar todo el objeto.
Por ejemplo:
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
)
)
)
)
)
Si hay un objeto cuya admins-> miembro de valor es igual a 11, eliminar el objeto y devolver la matriz sin este objeto. La matriz devuelta sería así:
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
)
)
)
)
)
¿Cómo podría hacer eso?
Tenga en cuenta ['array_filter'] (http://php.net/array_filter). –
Duplicado (http://stackoverflow.com/questions/7814872/how-to-filter-out-an-object-from-an-array-of-objects) – SparrowG