如何将相同的数组键合并为一个但值不同。?

时间:2016-11-04 12:51:04

标签: php arrays

如何将相同的数组键合并为一个,但值的方式不同。 我使用我的代码输出值如下:

  

Spanish Primera Division2016-17,2017-18 Copa Del Ray2016-17

但是当我回应我的代码时,我得到的输出如下:

  

Spanish Primera Division2017-18 Copa Del Ray2016-17 Spanish Primera   Division2016-17

你能两次见到西班牙Primera Division吗?我想要这一次但是一年两次。 代码:

git reset <path>

print_r输出:

<?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;
       }




?>

3 个答案:

答案 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;
     }
   }

答案 1 :(得分:0)

只需添加它们

$cc= $terms_competition + $terms_session;

$terms_competition中不存在$terms_session中不存在的任何键。

答案 2 :(得分:-1)

You can try like this :

With array_unique

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