What's wrong about this code?
($matches is a 2 dimensional array)

for($i = 0; $i < count($matches); $i++)
{
echo $matches[$i][0]->getName_team_home()."<br />";
}

It's not the actual code, it's just an example, originally it has to put the names of a team playing home in a table.

It only shows the first team, it doesn't seem like it goes any further than $i=0...

greets K?!

Dani AI

Generated

A very common cause for "only the first item shows" is how the outer array gets built. As discovered, the loop that prints the array can be fine while the code that fills the array accidentally overwrites a single slot instead of appending a new row each time. Inspecting the array shape before looping saves a lot of time.

Quick, practical checklist:

  • Dump the structure right after building it with print_r() or var_dump() to confirm how many outer elements and what the inner elements look like.
  • Make sure you append rows (for example with [] or array_push) instead of always assigning to the same index.
  • Prefer foreach for readability when iterating nested arrays; it avoids off-by-one mistakes and repeated count() calls.

Example patterns (two common approaches):

$matches = array();
while ($row = $result->fetch_assoc()) {
    $matches[] = array('home' => $row['home'], 'away' => $row['away']);
}

foreach ($matches as $match) {
    echo $match['home']; // or call a method if you stored objects
}

If you store objects inside the inner arrays, consider named keys ('home'/'away') rather than numeric indices to make code clearer and less error-prone. Also be careful with foreach by-reference (using &) — if you use references, unset() the loop variable afterward to avoid subtle reuse bugs.

See the PHP docs on arrays and counting for details and edge cases: PHP arrays manual and count().

Problem solved, the code itself worked, the problem was that i made a mistake putting the data from the database into this 2 dimensional array, everything was placed in $matches[0]...

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.