2011-04-27 79 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. 
    } 
}