簡體   English   中英

過濾掉數組中的對象

[英]Filtering out an object within an array

在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"
  }

}

在我的實際數組中,有[14]而不是[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可以幫助我進行調試。 我覺得第四行是錯過的地方。 預先感謝您的任何幫助,非常感謝。

它很簡單:

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

基本上,您的函數歸結為array_filter工作方式。

array_filter您有兩個參數。

$array, $callback

$array變量包含您要過濾的數組。
$callback是您希望為每個元素執行的函數,它將返回一個布爾值。 如果保留則為true,如果丟棄則為false。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM