Nikolas Trentin
Nikolas Trentin

Reputation: 1

Foreach loop doesn't echo anything

i'm working on my project. I have table with racers (ID, name, surname etc.) and I stored it in an array. Then i used foreach loop to echo this data, but nothing show up. This is my code:

$zavodnici_array = array();
while(false !== ($row = mysql_fetch_assoc($result))) {
$zavodnici_array[] = $row;
}  
foreach($zavodnici_array as $key) {
  echo $zavodnici_array[$key][id] ."<br>";
  echo $zavodnici_array[$key][jmeno] ."<br>";
  echo $zavodnici_array[$key][prijmeni] ."<br>";
}

Can anybody help me? :)

Upvotes: 0

Views: 121

Answers (2)

user2203117
user2203117

Reputation:

Since you are defining your own values for the array you must use this:

foreach ($array as $key => $value)

Upvotes: 0

dm03514
dm03514

Reputation: 55952

THere are a few things wrong with your example.

when using foreach as $key key is the value of each item in the array not the key

ass uming your mysql query fetched results

foreach($zavodnici_array as $key => $value) {
  echo $zavodnici_array[$key]['id'] ."<br>";
  echo $zavodnici_array[$key]['jmeno'] ."<br>";
  echo $zavodnici_array[$key]['prijmeni'] ."<br>";
}

or

foreach($zavodnici_array as $value) {
      echo $value['id'] ."<br>";
      echo $value['jmeno'] ."<br>";
      echo $value['prijmeni'] ."<br>";
    }

keys in php are strings or integers $value[id] is not valid. I assumed you were trying to type the index id

Upvotes: 2

Related Questions