如果存在公共值,则将数组内的元素组合在一起
I have this snippet of code:
foreach($data['matches'] as $record){
$matchday = $record['matchday'];
echo '<h4>Round'. $matchday .'</h4><br>'.$record['homeTeam']['name'].' - '.$record['awayTeam']['name'].'<br>';
}
It will loop over a multidimensional array to show all the calendar matches of a competition, matches
is the key who contain the array I'm using. I need to extract only once the matchday for every season round to display all the matches of every round under an header <h4>
like something round 1
, round 2
ecc. Every round have ten matches and if I use the loop to display also the information about the current round, it will duplicate this information, but I need to group them if they are under the same round. Is it possible to achieve this?
The idea is to obtain something like:
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..
now the data are displayed like this:
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..
EDIT Here is the array who I'm working on (I will take only two matches of the first round):
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) {
}
}
Simple, Take one extra variable and check that the current round and previous round value are same or not.
Something like below.
$roundName = "";
foreach($data['matches'] as $record){
$matchday = $record['matchday'];
if($roundName != $matchday){
echo '<h4>Round'. $matchday .'</h4><br>';
}
echo $record['homeTeam']['name'].' - '.$record['awayTeam']['name'].'<br>';
$roundName = $record['matchday'];
}
Other solution is,
Make one Multidimentional array with key as round name.
Something like,
$array['round1'][0] = 'team 1 - team 2';
$array['round1'][1] = 'team 3 - team 5';
$array['round2'][0] = 'team 1 - team 2';
$array['round2'][1] = 'team 3 - team 5';
................
................
................
................
$array['roundn'][n] = 'team 3 - team 5';