【问题标题】:Filtering out an object within an array过滤掉数组中的对象
【发布时间】: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


    【解决方案1】:

    就这么简单:

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

    【讨论】:

    • 非常感谢,@u_mulder !
    【解决方案2】:

    基本上,您的功能归结为array_filter 的工作原理。

    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';
    });
    

    那么matches数组将只填充符合导致回调函数返回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';
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-01
      • 2019-10-30
      • 2022-10-05
      • 1970-01-01
      • 2022-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多