Hi, i don't know how to do the following or even if it's possible.

These are 3 tables from my database sport:
[team]-|-----<-[team_match]->-----|-[match]

  • Team:
    • id (int)
    • name (text)
  • Team_match:
    • match_id (bigint)
    • team_id (int)
    • score (text) {this is not an integer because it can contain "f" for "forfait"}
    • home (tinyint) {this is a boolean, which indicates if the team plays at home for this match}
  • Match
    • id (bigint)
    • time (datetime)

With this setup, a match contains two entries of Team_match.
What i want to do in PHP is creating a two-dimensional array of matches, ordered alphabetically by the team's name.

To give you an example:
3 teams: Allstars, Bulls, Children
[Allstars vs. Bulls] [Allstars vs. Children]
[Bulls vs. Allstars] [Bulls vs. Children]
[Children vs. Allstars] [Children vs.Bulls]

How can i order the data first alphabetically by the team playing home, and then by the visitors?

Thanks in advance,
greets, Kenny.

Dani AI

Generated

Nice direction from and good point from — the robust pattern is to return one row per fixture with the home and visitor as columns, let the database order those two columns, then build your 2‑D array in PHP (or sort again in PHP if you need locale-aware ordering).

A reliable SQL pattern (self-join) that yields one row per match, ordered by home name then away name:

SELECT m.id AS match_id,
       th.name AS home_team,
       ta.name AS away_team,
       m.time
FROM `match` m
JOIN team_match thm ON thm.match_id = m.id AND thm.home = 1
JOIN team th ON th.id = thm.team_id
JOIN team_match tam ON tam.match_id = m.id AND tam.home = 0
JOIN team ta ON ta.id = tam.team_id
ORDER BY th.name COLLATE utf8mb4_general_ci, ta.name COLLATE utf8mb4_general_ci;

If you prefer to pivot in one pass (safer if flags are messy), use CASE + GROUP BY:

SELECT m.id AS match_id,
       MAX(CASE WHEN tm.home = 1 THEN t.name END) AS home_team,
       MAX(CASE WHEN tm.home = 0 THEN t.name END) AS away_team
FROM `match` m
JOIN team_match tm ON tm.match_id = m.id
JOIN team t ON t.id = tm.team_id
GROUP BY m.id
ORDER BY home_team, away_team;

Build the nested array in PHP and ensure keys are sorted:

$rows = $pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC);
$matrix = [];
foreach ($rows as $r) {
    $matrix[$r['home_team']][$r['away_team']] = [
        'match_id' => $r['match_id'],
        'time'     => $r['time'],
    ];
}
ksort($matrix, SORT_NATURAL | SORT_FLAG_CASE);
foreach ($matrix as &$sub) { ksort($sub, SORT_NATURAL | SORT_FLAG_CASE); }
unset($sub);

Troubleshooting notes:

  • Verify every match returns exactly two entries; use a quick GROUP BY on the link table to find anomalies.
  • Use COLLATE in SQL or PHP's Intl Collator for locale/case-sensitive ordering.
  • If scores or other fields aren't strictly numeric, keep them as strings in your output and only parse them where needed.

Recommended Answers

All 2 Replies

From the example you wrote, there isn't an order by home

Can you post an example that shows what you want?

Oh, i think my description was wrong. I meant that there should be 2 levels of sorting (maybe more, but these 2 levels are the problem).

Example: [Allstars vs. Bulls] contains
Team_match:
* match_id: 1
* team_id: 1
* score: 10
* home: 1 (true)
These are the "Allstars"
Team_match:
* match_id: 1
* team_id: 2
* score: 12
* home: 0 (false)
These are the "Bulls"

Then the sorting:
[Allstars vs. Bulls] [Allstars vs. Children]
[Bulls vs. Allstars] [Bulls vs. Children]
[Children vs. Allstars] [Children vs.Bulls]

In the first dimension of the array the teams playing home should be sorted alphabetically:
[Allstars vs. Bulls]
[Bulls vs. Allstars]
[Children vs. Allstars]

In the second dimension of the array the visitors should be sorted alphabetically:
[Allstars vs. Bulls] [Allstars vs. Children]

But i think i found the solution :). But i'm still doing some testing.

Greets, K?!.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.