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.
我有一个对象数组(如下所示),我想编写一个返回相同数组的函数,但是“object(s)”符合删除的标准。
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.
1-检查索引是否存在2-如果存在,检查所需的值,如果对象的索引等于该值,则删除整个对象。
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 :
如果有一个对象的admins-> member值等于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
)
)
)
)
)
How would I go about doing that?
我该怎么做呢?
3 个解决方案
#1
31
Want to filter an array? Use array_filter!
想要过滤数组?使用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;
});
#2
3
You can use array_filter
with a custom callback:
您可以将array_filter与自定义回调一起使用:
function filter_callback($element) {
if (isset($element->foo) && $element->foo == 'some_value') {
return TRUE;
}
return FALSE;
}
$arr = array_filter($arr, 'filter_callback');
#3
0
Provide another method:
提供另一种方法:
public function getArrayFiltered($aFilterKey, $aFilterValue, $array) {
$filtered_array = array();
foreach ($array as $value) {
if (isset($value->$aFilterKey)) {
if ($value->$aFilterKey == $aFilterValue) {
$filtered_array[] = $value;
}
}
}
return $filtered_array;
}
#1
31
Want to filter an array? Use array_filter!
想要过滤数组?使用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;
});
#2
3
You can use array_filter
with a custom callback:
您可以将array_filter与自定义回调一起使用:
function filter_callback($element) {
if (isset($element->foo) && $element->foo == 'some_value') {
return TRUE;
}
return FALSE;
}
$arr = array_filter($arr, 'filter_callback');
#3
0
Provide another method:
提供另一种方法:
public function getArrayFiltered($aFilterKey, $aFilterValue, $array) {
$filtered_array = array();
foreach ($array as $value) {
if (isset($value->$aFilterKey)) {
if ($value->$aFilterKey == $aFilterValue) {
$filtered_array[] = $value;
}
}
}
return $filtered_array;
}