Working within php, I am passed an array of objects ( $terms ):
在php中工作时,会向我传递一个对象数组($terms):
array(15) {
[0]=>
object(WP_Term)#341 (10) {
["term_id"]=>
int(263)
["name"]=>
string(15) "Moo"
["slug"]=>
string(15) "moo"
["term_group"]=>
int(0)
["term_taxonomy_id"]=>
int(263)
["taxonomy"]=>
string(9) "my_topics"
["description"]=>
string(0) ""
["parent"]=>
int(0)
["count"]=>
int(29)
["filter"]=>
string(3) "raw"
}
[1]=>
object(WP_Term)#342 (10) {
["term_id"]=>
int(264)
["name"]=>
string(10) "Bark"
["slug"]=>
string(10) "bark"
["term_group"]=>
int(0)
["term_taxonomy_id"]=>
int(264)
["taxonomy"]=>
string(9) "my_topics"
["description"]=>
string(0) ""
["parent"]=>
int(0)
["count"]=>
int(17)
["filter"]=>
string(3) "raw"
}
[2]=>
object(WP_Term)#343 (10) {
["term_id"]=>
int(281)
["name"]=>
string(16) "Meow"
["slug"]=>
string(16) "meow"
["term_group"]=>
int(0)
["term_taxonomy_id"]=>
int(281)
["taxonomy"]=>
string(9) "my_topics"
["description"]=>
string(0) ""
["parent"]=>
int(266)
["count"]=>
int(2)
["filter"]=>
string(3) "raw"
}
[3]=>
object(WP_Term)#344 (10) {
["term_id"]=>
int(282)
["name"]=>
string(19) "Tweet"
["slug"]=>
string(19) "tweet"
["term_group"]=>
int(0)
["term_taxonomy_id"]=>
int(282)
["taxonomy"]=>
string(9) "my_topics"
["description"]=>
string(0) ""
["parent"]=>
int(266)
["count"]=>
int(4)
["filter"]=>
string(3) "raw"
}
[4]=>
object(WP_Term)#345 (10) {
["term_id"]=>
int(772)
["name"]=>
string(8) "Chirp"
["slug"]=>
string(8) "chirp"
["term_group"]=>
int(0)
["term_taxonomy_id"]=>
int(772)
["taxonomy"]=>
string(9) "my_topics"
["description"]=>
string(0) ""
["parent"]=>
int(0)
["count"]=>
int(3)
["filter"]=>
string(3) "raw"
}
}
}
In my real array, instead of [4], there are [14]...but that shouldn't matter because I can't rely on targeting by number.
在我的实数组中,有[4],有[14]…但这并不重要,因为我不能依靠数量来确定目标。
If an object within the array contains "slug" value of "meow", I want to filter that object out and produce a new array with the rest of the objects in tact.
如果数组中的一个对象包含“slug”值的“喵”,我想将该对象过滤掉,并与其余对象一起生成一个新的数组。
I need to exclude a specific object which has a specific value, in the array. My approach is to use 'array_filter' Here's where I am stuck (I feel like I'm so close but traversing arrays of objects can give me a hard time):
我需要排除一个特定的对象,它有一个特定的值,在数组中。我的方法是使用“array_filter”,这就是我被困的地方(我感觉自己离得很近,但遍历对象数组会让我很困难):
$refinedterms = array_filter($terms, function($obj){
echo objTEST;
var_dump($obj);
foreach($obj->WP_Term as $wpTermObj){
echo wpTermObjTEST;
var_dump($wpTermObj);
foreach ($wpTermObj->slug as $slug) {
echo slugTEST;
var_dump($slug);
if ($slug == 'meow') return false;
}
}
return true;
});
The echos and var_dumps are in place to help me debug. I feel like the fourth line is where something is a miss. Thank you in advance for any assistance, it is very much appreciated.
echos和var_dumps可以帮助我进行调试。我觉得第四行好像有什么东西是错的。谢谢您的帮助,非常感谢。
2 个解决方案
#1
4
It is as simple as:
很简单:
$new_array = array_filter(
$terms,
function($v) { return $v->slug !== 'meow'; }
);
#2
0
Basically your function boils down to how does array_filter
work.
基本上,你的函数可以归结为array_filter是如何工作的。
in array_filter
you have two arguments.
在array_filter中,有两个参数。
$array, $callback
The $array
variable contains an array you wish to filter.
The $callback
is a function you wish to perform for each element, which will return a boolean value. True if keep, false if discard.
$array变量包含要筛选的数组。$callback是您希望为每个元素执行的函数,它将返回一个布尔值。保持为真,放弃为假。
The array_filter
function itself will loop over each and every element and pass the current element to the callback function.
array_filter函数本身将对每个元素进行循环,并将当前元素传递给回调函数。
In basis the array_filter
function performs this:
根据array_filter函数执行以下操作:
$keep = [];
foreach($array as $item) {
if($callback($item)) {
$keep[] = $item;
}
}
return $keep;
Thus in your callback method you only need to evaluate if the current passed on item matches your criteria. If it does, you can return true. If it doesn't return false.
因此,在回调方法中,您只需要评估当前传入的项是否符合您的条件。如果是,你可以返回true。如果它不返回false。
$matches = array_filter($terms, function($item) {
return $item->slug != 'meow';
});
Then the matches array will only be filled with items that match the criteria that causes the callback function to return true
然后,匹配数组将只填充与导致回调函数返回true的条件匹配的项
$cats = array_filter($terms, function($item) {
return $item->slug == 'meow';
});
$birds = array_filter($terms, function($item) {
return $item->slug == 'tweet';
});
$raining_dogs_and_cats = array_filter($terms, function($item) {
return $item->slug == 'bark' || $item->slug == 'meow';
});
#1
4
It is as simple as:
很简单:
$new_array = array_filter(
$terms,
function($v) { return $v->slug !== 'meow'; }
);
#2
0
Basically your function boils down to how does array_filter
work.
基本上,你的函数可以归结为array_filter是如何工作的。
in array_filter
you have two arguments.
在array_filter中,有两个参数。
$array, $callback
The $array
variable contains an array you wish to filter.
The $callback
is a function you wish to perform for each element, which will return a boolean value. True if keep, false if discard.
$array变量包含要筛选的数组。$callback是您希望为每个元素执行的函数,它将返回一个布尔值。保持为真,放弃为假。
The array_filter
function itself will loop over each and every element and pass the current element to the callback function.
array_filter函数本身将对每个元素进行循环,并将当前元素传递给回调函数。
In basis the array_filter
function performs this:
根据array_filter函数执行以下操作:
$keep = [];
foreach($array as $item) {
if($callback($item)) {
$keep[] = $item;
}
}
return $keep;
Thus in your callback method you only need to evaluate if the current passed on item matches your criteria. If it does, you can return true. If it doesn't return false.
因此,在回调方法中,您只需要评估当前传入的项是否符合您的条件。如果是,你可以返回true。如果它不返回false。
$matches = array_filter($terms, function($item) {
return $item->slug != 'meow';
});
Then the matches array will only be filled with items that match the criteria that causes the callback function to return true
然后,匹配数组将只填充与导致回调函数返回true的条件匹配的项
$cats = array_filter($terms, function($item) {
return $item->slug == 'meow';
});
$birds = array_filter($terms, function($item) {
return $item->slug == 'tweet';
});
$raining_dogs_and_cats = array_filter($terms, function($item) {
return $item->slug == 'bark' || $item->slug == 'meow';
});