【发布时间】:2018-09-28 19:00:16
【问题描述】:
在 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"
}
}
在我的真实数组中,不是 [4],而是 [14]...但这不重要,因为我不能依赖按数字定位。
如果数组中的一个对象包含“meow”的“slug”值,我想过滤掉该对象并生成一个新数组,其余对象完好无损。
我需要在数组中排除具有特定值的特定对象。我的方法是使用'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;
});
echos 和 var_dumps 可以帮助我进行调试。我觉得第四行是遗漏的地方。提前感谢您的任何帮助,非常感谢。
【问题讨论】:
标签: php arrays wordpress object