一千萬個為什麽

搜索

查找具有特定值的數組多維數組wordpress in_array

我正在運行一個使用wpml的wordpress網站,它給了我很多問題,試圖對我在翻譯的帖子上的標簽運行查詢。

所以我想要做的是一個小小的黑客,這將允許我查詢所有帖子,並使用wordpresses函數get_the_tags();找到具有特定標簽的帖子

我試圖使用in_array,它似乎不適用於wordpress輸出的multidimesional數組,這裏是print_r()的數組;

>   Array (
>     [629] => stdClass Object
>         (
>             [term_id] => 629
>             [name] => bulletin
>             [slug] => bulletin
>             [term_group] => 0
>             [term_taxonomy_id] => 630
>             [taxonomy] => post_tag
>             [description] => 
>             [parent] => 0
>             [count] => 2
>             [object_id] => 19838
>         )
> 
>     [631] => stdClass Object
>         (
>             [term_id] => 631
>             [name] => english2
>             [slug] => english2
>             [term_group] => 0
>             [term_taxonomy_id] => 632
>             [taxonomy] => post_tag
>             [description] => 
>             [parent] => 0
>             [count] => 1
>             [object_id] => 19838
>         )
> 
> ) Array (
>     [629] => stdClass Object
>         (
>             [term_id] => 629
>             [name] => bulletin
>             [slug] => bulletin
>             [term_group] => 0
>             [term_taxonomy_id] => 630
>             [taxonomy] => post_tag
>             [description] => 
>             [parent] => 0
>             [count] => 2
>             [object_id] => 19842
>         )
> 
>     [630] => stdClass Object
>         (
>             [term_id] => 630
>             [name] => english1
>             [slug] => english1
>             [term_group] => 0
>             [term_taxonomy_id] => 631
>             [taxonomy] => post_tag
>             [description] => 
>             [parent] => 0
>             [count] => 1
>             [object_id] => 19842
>         )
> 
> ) Array (
>     [0] => stdClass Object
>         (
>             [term_id] => 633
>             [name] => welsh2
>             [slug] => welsh2
>             [term_group] => 0
>             [term_taxonomy_id] => 634
>             [taxonomy] => post_tag
>             [description] => 
>             [parent] => 0
>             [count] => 1
>         )
> 
> ) Array (
>     [0] => stdClass Object
>         (
>             [term_id] => 632
>             [name] => welsh1
>             [slug] => welsh1
>             [term_group] => 0
>             [term_taxonomy_id] => 633
>             [taxonomy] => post_tag
>             [description] => 
>             [parent] => 0
>             [count] => 1
>         )
> 
> )

這是我的代碼我只希望它找到名稱為welsh1的數組,這是數組中的最後一個。

 //Global calls to the database
  global $wpdb;

 //Runs a query to get all results from the wp_posts table
  $all = $wpdb->get_results( "SELECT * FROM wp_posts" );

 //loops through each one   
  foreach($all as $v){

      $tags = get_the_tags($v->ID);

      if (in_array('welsh1', $tags)) {
        echo "'ph' was found\n";
      }

      echo "
";
      print_r($tags);
      echo "
";
  }

最佳答案

$ tags是一個對象數組,而不是多維數組。

The following code should identify the string welsh1

foreach($tags as $tag){
  if ($tag->name == "welsh1" || $tag->slug == "welsh1"){
     echo "'ph' was found\n";
     break;//this line makes the foreach loop end after first success.
  }
}

轉載註明原文: 查找具有特定值的數組多維數組wordpress in_array