过滤掉数组中的对象

时间:2018-04-18 13:44:02

标签: php arrays wordpress object

在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] ......但这不重要,因为我不能依靠数字定位。

如果数组中的对象包含" slug" " meow"的价值,我想过滤掉那个对象并生成一个新的数组,其余的对象都是明确的。

我需要在数组中排除具有特定值的特定对象。我的方法是使用' 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_dump用于帮助我调试。我觉得第四行是一个错过的地方。提前感谢您的任何帮助,非常感谢。

2 个答案:

答案 0 :(得分:4)

这很简单:

$new_array = array_filter(
    $terms, 
    function($v) { return $v->slug !== 'meow'; }
);

答案 1 :(得分:0)

基本上,您的功能归结为array_filter如何运作。

<{1>} array_filter你有两个论点。

$array, $callback

$array变量包含您要过滤的数组 $callback是您希望为每个元素执行的函数,它将返回一个布尔值。如果保留则为真,如果丢弃则为假。

array_filter函数本身将循环遍历每个元素,并将当前元素传递给回调函数。

array_filter函数的基础上执行此操作:

$keep = [];
foreach($array as $item) {
    if($callback($item)) {
         $keep[] = $item;
    }
}
return $keep;

因此,在您的回调方法中,您只需要评估当前传递的项目是否符合您的条件。如果是,您可以返回true。如果它没有返回false。

$matches = array_filter($terms, function($item) {
   return $item->slug != 'meow';
});

然后匹配数组将只填充符合导致回调函数返回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';
});