PHP: creating HTML table from nested array (json) but getting results AND "invalid argument supplied for foreach()" -
i trying create html table array obtained through api/json.
it's nested array (the actual data nested within session)
array ( [sessid] => -hnkgi-k1rgwhnymkcscr0bom-rrkurn2s1pgmzobx4 [status] => 0 [players] => array ( [0] => array ( [first_name] => chris [last_name] => clausing [pdga_number] => 31635 [rating] => 930 [year] => 2014 [class] => [gender] => m [bracket] => open [country] => nl [state_prov] => [tournaments] => 11 [rating_rounds_used] => 36 [points] => 998 [prize] => 0.00 ) [1] => array ( [first_name] => uwe walter [last_name] => schlueter [pdga_number] => 37788 [rating] => 902 [year] => 2014 [class] => [gender] => m [bracket] => master [country] => nl [state_prov] => [tournaments] => 12 [rating_rounds_used] => 33 [points] => 970 [prize] => 0.00 ) [2] => array ( [first_name] => mark [last_name] => steenhuis [pdga_number] => 50574 [rating] => 859 [year] => 2014 [class] => [gender] => m [bracket] => master [country] => nl [state_prov] => [tournaments] => 12 [rating_rounds_used] => 28 [points] => 678 [prize] => 0.00 ) ) )
and warning mesages:
warning: invalid argument supplied foreach() in /home/surrealm/dvh/player_statistics.php on line 103 warning: invalid argument supplied foreach() in /home/surrealm/dvh/player_statistics.php on line 103
see http://vliegende-hollander.com/player_statistics.php?year=2014&class=a&gender=m&country=nl
as can see can extract desired data, , create html table it. but, can not rid of 2 error messages.
if take away 1 of foreach() first character of first value (in case, first character of sessid).
the actual bit of php i'm using create table is:
<? if (is_array($player)) { foreach($player $key){ foreach($key $k){ // <- line 103 echo "<tr>"; echo "<td>".$k['pdga_number']."</td>"; echo "<td>".$k['first_name']." ".$k['last_name']."</td>"; echo "<td>".$k['country']."</td>"; echo "<td>".$k['rating']."</td>"; echo "</tr>"; } } } ?>
can me clean code, rid of 2 warnings?
i'm not full-time programmer, it's not understanding foreach() function. extract , display data using mysql, , while() loops instead.
the error states $key
variable passed each isn't array. work player
array have have arrays within (i.e. multidimensional array), second foreach
can iterate through it.
if player array doesn't have arrays within it, need one foreach
loop through , can access data using index (i.e. $key['pdga_number']
instead of $k['pdga_number']
).
for me though, need know contents of $player
array.
edit
i've seen example given, try doing second if(is_array($key)
before calling second foreach, i.e.
if (is_array($player)) { foreach($player $key){ if(is_array($key)){ foreach($key $k){ // <- line 103 echo "<tr>"; echo "<td>".$k['pdga_number']."</td>"; echo "<td>".$k['first_name']." ".$k['last_name']."</td>"; echo "<td>".$k['country']."</td>"; echo "<td>".$k['rating']."</td>"; echo "</tr>"; } } } }
Comments
Post a Comment