douchi2022 2018-04-18 13:44
浏览 119
已采纳

过滤掉数组中的对象

Working within php, I am passed an array of objects ( $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.

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.

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):

$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.

  • 写回答

2条回答 默认 最新

  • dongshi4589 2018-04-18 13:47
    关注

    It is as simple as:

    $new_array = array_filter(
        $terms, 
        function($v) { return $v->slug !== 'meow'; }
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?