如果存在共同值,则将数组内的元素分组

时间:2018-08-17 11:53:21

标签: php arrays foreach

我有以下代码段:

foreach($data['matches'] as $record){
    $matchday = $record['matchday'];
    echo '<h4>Round'. $matchday  .'</h4><br>'.$record['homeTeam']['name'].'&nbsp;-&nbsp;'.$record['awayTeam']['name'].'<br>';
}

它将循环遍历多维数组以显示比赛的所有日历匹配,matches是包含我正在使用的数组的键。我只需要提取每个赛季的比赛日一次,即可在标题<h4>round 1round 2等下显示每一轮的所有比赛。每个回合都有十个匹配项,如果我使用循环显示有关当前回合的信息,它将重复此信息,但是如果它们在同一回合下,则需要对它们进行分组。有可能实现这一目标吗? 这个想法是获得类似的东西:

Round 1
team 1 - team 2
team 3 - team 4
team 5 - team 6
Round 2
team 1 - team 2
team 3 - team 4
team 5 - team 6
ecc..

现在数据显示如下:

Round 1
team 1 - team 2
Round 1
team 3 - team 4
Round 1
team 5 - team 6
Round 2
team 1 - team 2
Round 2
team 3 - team 4
Round 2
team 5 - team 6
ecc..

编辑 这是我正在处理的数组(我将只参加第一轮的两场比赛):

    array(4) {
  ["count"]=>
  int(380)
  ["filters"]=>
  array(0) {
  }
  ["competition"]=>
  array(6) {
    ["id"]=>
    int(2019)
    ["area"]=>
    array(2) {
      ["id"]=>
      int(2114)
      ["name"]=>
      string(5) "Italy"
    }
    ["name"]=>
    string(7) "Serie A"
    ["code"]=>
    NULL
    ["plan"]=>
    string(8) "TIER_ONE"
    ["lastUpdated"]=>
    string(20) "2018-06-05T00:14:16Z"
  }
  ["matches"]=>
  array(380) {
    [0]=>
    array(12) {
      ["id"]=>
      int(247791)
      ["season"]=>
      array(4) {
        ["id"]=>
        int(290)
        ["startDate"]=>
        string(10) "2018-08-18"
        ["endDate"]=>
        string(10) "2019-05-26"
        ["currentMatchday"]=>
        int(1)
      }
      ["utcDate"]=>
      string(20) "2018-08-18T16:00:00Z"
      ["status"]=>
      string(9) "SCHEDULED"
      ["matchday"]=>
      int(1)
      ["stage"]=>
      string(14) "REGULAR_SEASON"
      ["group"]=>
      string(14) "Regular Season"
      ["lastUpdated"]=>
      string(20) "2018-08-01T18:02:07Z"
      ["homeTeam"]=>
      array(2) {
        ["id"]=>
        int(106)
        ["name"]=>
        string(16) "AC Chievo Verona"
      }
      ["awayTeam"]=>
      array(2) {
        ["id"]=>
        int(109)
        ["name"]=>
        string(11) "Juventus FC"
      }
      ["score"]=>
      array(6) {
        ["winner"]=>
        NULL
        ["duration"]=>
        string(7) "REGULAR"
        ["fullTime"]=>
        array(2) {
          ["homeTeam"]=>
          NULL
          ["awayTeam"]=>
          NULL
        }
        ["halfTime"]=>
        array(2) {
          ["homeTeam"]=>
          NULL
          ["awayTeam"]=>
          NULL
        }
        ["extraTime"]=>
        array(2) {
          ["homeTeam"]=>
          NULL
          ["awayTeam"]=>
          NULL
        }
        ["penalties"]=>
        array(2) {
          ["homeTeam"]=>
          NULL
          ["awayTeam"]=>
          NULL
        }
      }
      ["referees"]=>
      array(0) {
      }
    }
    [1]=>
    array(12) {
      ["id"]=>
      int(247786)
      ["season"]=>
      array(4) {
        ["id"]=>
        int(290)
        ["startDate"]=>
        string(10) "2018-08-18"
        ["endDate"]=>
        string(10) "2019-05-26"
        ["currentMatchday"]=>
        int(1)
      }
      ["utcDate"]=>
      string(20) "2018-08-18T18:30:00Z"
      ["status"]=>
      string(9) "SCHEDULED"
      ["matchday"]=>
      int(1)
      ["stage"]=>
      string(14) "REGULAR_SEASON"
      ["group"]=>
      string(14) "Regular Season"
      ["lastUpdated"]=>
      string(20) "2018-08-01T18:02:07Z"
      ["homeTeam"]=>
      array(2) {
        ["id"]=>
        int(110)
        ["name"]=>
        string(8) "SS Lazio"
      }
      ["awayTeam"]=>
      array(2) {
        ["id"]=>
        int(113)
        ["name"]=>
        string(10) "SSC Napoli"
      }
      ["score"]=>
      array(6) {
        ["winner"]=>
        NULL
        ["duration"]=>
        string(7) "REGULAR"
        ["fullTime"]=>
        array(2) {
          ["homeTeam"]=>
          NULL
          ["awayTeam"]=>
          NULL
        }
        ["halfTime"]=>
        array(2) {
          ["homeTeam"]=>
          NULL
          ["awayTeam"]=>
          NULL
        }
        ["extraTime"]=>
        array(2) {
          ["homeTeam"]=>
          NULL
          ["awayTeam"]=>
          NULL
        }
        ["penalties"]=>
        array(2) {
          ["homeTeam"]=>
          NULL
          ["awayTeam"]=>
          NULL
        }
      }
      ["referees"]=>
      array(0) {
      }
    }
    [2]=>
    array(12) {
      ["id"]=>
      int(247790)
      ["season"]=>
      array(4) {
        ["id"]=>
        int(290)
        ["startDate"]=>
        string(10) "2018-08-18"
        ["endDate"]=>
        string(10) "2019-05-26"
        ["currentMatchday"]=>
        int(1)
      }
      ["utcDate"]=>
      string(20) "2018-08-19T16:00:00Z"
      ["status"]=>
      string(9) "SCHEDULED"
      ["matchday"]=>
      int(1)
      ["stage"]=>
      string(14) "REGULAR_SEASON"
      ["group"]=>
      string(14) "Regular Season"
      ["lastUpdated"]=>
      string(20) "2018-08-01T18:02:07Z"
      ["homeTeam"]=>
      array(2) {
        ["id"]=>
        int(586)
        ["name"]=>
        string(9) "Torino FC"
      }
      ["awayTeam"]=>
      array(2) {
        ["id"]=>
        int(100)
        ["name"]=>
        string(7) "AS Roma"
      }
      ["score"]=>
      array(6) {
        ["winner"]=>
        NULL
        ["duration"]=>
        string(7) "REGULAR"
        ["fullTime"]=>
        array(2) {
          ["homeTeam"]=>
          NULL
          ["awayTeam"]=>
          NULL
        }
        ["halfTime"]=>
        array(2) {
          ["homeTeam"]=>
          NULL
          ["awayTeam"]=>
          NULL
        }
        ["extraTime"]=>
        array(2) {
          ["homeTeam"]=>
          NULL
          ["awayTeam"]=>
          NULL
        }
        ["penalties"]=>
        array(2) {
          ["homeTeam"]=>
          NULL
          ["awayTeam"]=>
          NULL
        }
      }
      ["referees"]=>
      array(0) {
      }
    }

1 个答案:

答案 0 :(得分:0)

简单,增加一个变量,并检查当前回合和前一回合值是否相同。

类似下面的内容。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="start"
    android:layout_marginStart="5dp"
    android:orientation="vertical"
    android:weightSum=".9">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.3"
        android:text="@string/status_heading"
        android:textSize="11sp" />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.3"
        android:src="@drawable/ic_launcher_foreground" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.3"
        android:text="test"
        android:textSize="15sp" />
</LinearLayout>

其他解决方案是

使用键作为回合名称制作一个多维数组。

类似

$roundName = "";

foreach($data['matches'] as $record){
    $matchday = $record['matchday'];
    if($roundName != $matchday){
       echo '<h4>Round'. $matchday  .'</h4><br>';
    }
    echo $record['homeTeam']['name'].'&nbsp;-&nbsp;'.$record['awayTeam']['name'].'<br>';
    $roundName = $record['matchday'];
}