2016-03-19 27 views
0

HI我的代碼不能正常工作,我得到的代碼時運行這個錯誤:PHP錯誤的參數不是數組錯誤

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中的整行添加到數組結果中。這個數組然後將顯示在一個表中給用戶。

如果有人能給我一個很好的解決方案。

+3

好,'$ GAME_LIST [$行] [$山坳]'是一個字符串,不是一個數組。你爲什麼不簡單地做:'$ result [] = $ game_list [$ row];'? – Jeff

回答

0

該錯誤的原因是,您嘗試將數組與字符串合併, array_merge將兩個數組作爲參數。

我建議不要使用array_merge()可言,而只是請求的數據添加到結果數組:

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

因爲當我這樣做時,我得到的錯誤:致命錯誤:不能使用[]讀取第110行 – UnknownPerson

+0

我不相信你使用了我在這裏顯示的(確切)代碼,因爲沒有什麼* $結果[]'。唯一的可能性可能是,'$ row'是未定義的,但我仍然認爲它會拋出一個不同的錯誤。 – Jeff