2011-04-27 92 views
0

我运行一个WordPress网站使用WPML这是给我的努力在我翻译的帖子这是在威尔士CY运行标记的查询问题负载。查找阵列某一个值,多维数组WordPress的in_array

那么什么,我试图做的是一个小黑客,让我查询所有帖子,并使用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 "<pre>"; 
     print_r($tags); 
     echo "</pre>"; 
    } 

回答

1

$ tags是一个对象数组,而不是一个多维数组。

下面的代码应确定字串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. 
    } 
}