我有一個對象數組(下面顯示),我想寫一個函數返回相同的數組,但與符合條件刪除的「對象」。如何篩選對象數組?
則該函數將:
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
)
)
)
)
)
我該怎麼做呢?
看看['array_filter'](http://php.net/array_filter)。 –
Duplicate(http://stackoverflow.com/questions/7814872/how-to-filter-out-an-object-from-an-array-of-objects) – SparrowG