2016-11-04 114 views
0

如何合併相同的數組鍵,但數值不同。 我用我的代碼輸出值如下:如何合併相同的數組鍵,但數值不同。

西甲Division2016-17,2017-18國王杯Ray2016-17

但是當我回聲出我的代碼,然後我得到輸出如下:

西甲Division2017-18國王杯Ray2016-17西甲 Division2016-17

你能看兩次西班牙西甲賽區嗎?我想這一次,但這一年兩次。 代碼:

<?php 
     $champion_team = get_post_meta(get_the_ID(), 'football_league_team_name', true); 
     $terms_competition = get_the_terms(get_the_ID(), 'competition'); 
     $terms_session = get_the_terms(get_the_ID(), 'session'); 
     $cc= array_merge($terms_competition, $terms_session); 

     foreach ($cc as $c) { 
     # code... 
     echo $c->name; 
     } 




?> 

print_r的輸出:

WP_Term Object ([term_id] => 6 [name] => Spanish Primera Division [slug] => spanish-primera-divisioj [term_group] => 0 [term_taxonomy_id] => 6 [taxonomy] => competition [description] => [parent] => 0 [count] => 10 [filter] => raw) 

WP_Term Object ([term_id] => 7 [name] => 2017-18 [slug] => 2017-18 [term_group] => 0 [term_taxonomy_id] => 7 [taxonomy] => session [description] => [parent] => 0 [count] => 4 [filter] => raw) 

WP_Term Object ([term_id] => 18 [name] => Copa Del Ray [slug] => copa-del-ray [term_group] => 0 [term_taxonomy_id] => 18 [taxonomy] => competition [description] => [parent] => 0 [count] => 1 [filter] => raw) 

WP_Term Object ([term_id] => 11 [name] => 2016-17 [slug] => 2016-17 [term_group] => 0 [term_taxonomy_id] => 11 [taxonomy] => session [description] => [parent] => 0 [count] => 9 [filter] => raw) 

WP_Term Object ([term_id] => 6 [name] => Spanish Primera Division [slug] => spanish-primera-divisioj [term_group] => 0 [term_taxonomy_id] => 6 [taxonomy] => competition [description] => [parent] => 0 [count] => 10 [filter] => raw) 

WP_Term Object ([term_id] => 11 [name] => 2016-17 [slug] => 2016-17 [term_group] => 0 [term_taxonomy_id] => 11 [taxonomy] => session [description] => [parent] => 0 [count] => 9 [filter] => raw) 

回答

0

只需添加他們

$cc= $terms_competition + $terms_session; 

不存在於$terms_competition中的任何密鑰都是從$terms_session開始添加的。

-1

You can try like this : 
 

 
With array_unique 
 

 
foreach (array_unique($data) as $d) { 
 
// Do stuff with $d ... 
 
}

+0

謝謝。通過這種方式? –

0
<?php 
    $champion_team = get_post_meta(get_the_ID(), 'football_league_team_name', true); 
    $terms_competition = get_the_terms(get_the_ID(), 'competition'); 
    $terms_session = get_the_terms(get_the_ID(), 'session'); 
    $cc= array_merge($terms_competition, $terms_session); 

    $temp = array(); 
    foreach ($cc as $c) { 
    # code... 

    if(!in_array($temp)) { 
     echo $c->name; 
     $temp[] = $c->term_id; 
    } 
    } 
+0

顯示:警告:in_array()需要至少2個參數,1在C:\ wamp \ www \ wordpress \ wp-content \ themes \ testtheme \ framework \ team \ honour.php中給出,位於第48行 –

相關問題