dongyun234854 2015-04-22 17:05 采纳率: 0%
浏览 67
已采纳

PHP:从嵌套数组(json)创建HTML表但获取结果和“为foreach()提供的无效参数”

I am trying to create a HTML table from an array that is obtained through api/json.

It's a nested array (the actual data is nested within the 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] => A
                [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] => A
                [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] => A
                [gender] => M
                [bracket] => Master
                [country] => NL
                [state_prov] => 
                [tournaments] => 12
                [rating_rounds_used] => 28
                [points] => 678
                [prize] => 0.00
            )
      )
)

And the warning mesages:

Warning: Invalid argument supplied for foreach() in /home/surrealm/dvh/player_statistics.php on line 103

Warning: Invalid argument supplied for 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 you can see i can extract the desired data, and create the HTML table from it. But, I can not get rid of the two error messages.

If i take away one of the foreach() i only get first character of first value only (in this case, the first character of the sessid).

The actual bit of PHP i'm using to create the table is:

<?

if (is_array($player)) {
 foreach($player as $key){
  foreach($key as $k){             // <- this is 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 anyone help me clean up this code, so i get rid of the two warnings?

I'm not a full-time programmer, so it's probably just my not fully understanding the foreach() function. I usually extract and display data using MySQL, and while() loops instead.

  • 写回答

1条回答 默认 最新

  • dongren5293 2015-04-22 17:19
    关注

    The error states that the $key variable that you passed into the for each isn't an array. For this to work the player array would have to have arrays within it (i.e. be a multidimensional array), so the second foreach can iterate through it.

    If your player array doesn't have arrays within it, then you only need one foreach to loop through and then you can access the data using the index (i.e. $key['pdga_number'] instead of $k['pdga_number']).

    For me to fully help though, I would need to know the contents of the $player array.

    EDIT

    I've just seen the example given, try doing a second if(is_array($key) before calling the second foreach, i.e.

    if (is_array($player)) {
        foreach($player as $key){
            if(is_array($key)){
                foreach($key as $k){             // <- this is 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>";
                }
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端