【问题标题】:PHP Error Arguments Not an Array ErrorPHP 错误参数不是数组错误
【发布时间】:2016-03-19 14:55:42
【问题描述】:

您好,我的代码不工作,运行代码时出现此错误:

Picture of error

这是代码的第 110 行:

$results = array_merge($results, $game_list[$row][$col]);

这是剩下的代码:

$results = array();

        for ($row = 0; $row < $num_rows; $row++){

                            if (strstr($game_list[$row][2], $search) or strstr($game_list[$row][3], $search)){
                                   for ($col = 0; $col < count($game_list[$row]); $col ++) { 

                                           $results = array_merge($results, $game_list[$row][$col]);




                                           $successful = true;
                                   }
                            }


        }
                    if ($successful == true){
                            echo "<table>

                                <tr>
                                    <th>Game ID</th>
                                    <th>Genre</th>
                                    <th>Game Name</th>
                                    <th>Game Description</th>
                                    <th>Rental Cost Per Day</th>
                                </tr>";

                                // Set number of table rows
                                $num_rows = count($results) - 1;
                                // Set number of table columns
                                $num_cols = 5;

                                // Start loop to generate rows
                                for($row = 0; $row < $num_rows; $row++) {
                                    // Generate row HTML
                                    echo "<tr>";

                                    //Start loop to generate columns (nested FOR loop!)
                                    for($col = 0 ; $col < $num_cols; $col++) {
                                            // Generate column HTML
                                            echo "<td>". $results[$row][$col] ."</td>";
                                    }
                                // End of columns loop
                                // Generate end of row HTML
                                echo "</tr>";
                                }

                            echo "</table>";

该代码用于搜索数组 game_list 并查看用户输入的关键字是否在数组中。如果是,则代码将获取数组 game_list 中的整行并将其添加到数组结果中。然后,该数组将在表格中显示给用户。

如果有人可以给我一个很好的解决方案。

【问题讨论】:

  • 好吧,$game_list[$row][$col] 是一个字符串,而不是一个数组。你为什么不干脆做:$result[] = $game_list[$row];

标签: php arrays arguments


【解决方案1】:

该错误的原因是,您尝试将数组与字符串合并, array_merge 接受两个数组作为参数。

我建议根本不要使用array_merge(),而只需将请求的数据添加到结果数组中:

<?php
$results = Array();
...
   $results[] = $game_list[$row];
...
?>

【讨论】:

  • 因为当我这样做时,我得到错误:致命错误:无法使用 [] 读取第 110 行
  • 我不相信你使用了我在这里展示的(确切的)代码,因为没有任何东西可以*读取'$results[]。唯一的可能是,$row 未定义,但我仍然认为它会引发不同的错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-08-26
  • 2011-02-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-22
  • 1970-01-01
相关资源
最近更新 更多