2015-04-26 84 views
0

我想在PHP中以數組的形式製作一些假數據,並在運行此行時不斷獲得非法偏移類型錯誤,任何人都可以幫助解釋原因嗎?PHP創建陣列時非法偏移類型

我查找了非法偏移錯誤的原因,它似乎並不像我通過偏移量訪問任何數據,我嘗試創建一個數組,並且沒有在哪裏可以看到我正在使用對象的位置作爲數組的關聯。

$fake_data = array(
        ["game_id"] => "1", 
        ["turn_number"] => "1", 
        ["host_user"] => array(
              ["units"] => array(
                   array("row"=> "1", "col" => "1", "hp" => "100", "armor" => "100", "is_dead" => "0", "direction_facing" => "1", "name" => "AssaultAlpha"), 
                   array("row"=> "1", "col" => "2", "hp" => "100", "armor" => "100", "is_dead" => "0", "direction_facing" => "1", "name" => "JuggernautAlpha"), 
                   array("row"=> "1", "col" => "3", "hp" => "100", "armor" => "100", "is_dead" => "0", "direction_facing" => "1", "name" => "MedicAlpha") 

                  ), 
              ["hit_indicators"] => array(
                     array("row"=> "1", "col" => "3", "direction" => "1"), 
                     array("row"=> "1", "col" => "2", "direction" => "1") 
                   ) 
             ), 
        ["client_user"] => array(
              ["units"] => array(
                   array("row"=> "5", "col" => "1", "hp" => "100", "armor" => "100", "is_dead" => "0", "direction_facing" => "1", "name" => "AssaultAlpha"), 
                   array("row"=> "6", "col" => "2", "hp" => "100", "armor" => "100", "is_dead" => "0", "direction_facing" => "1", "name" => "JuggernautAlpha"), 
                   array("row"=> "7", "col" => "3", "hp" => "100", "armor" => "100", "is_dead" => "0", "direction_facing" => "1", "name" => "MedicAlpha") 

                  ), 
              ["hit_indicators"] => array(
                     array("row"=> "5", "col" => "3", "direction" => "1"), 
                     array("row"=> "6", "col" => "2", "direction" => "1") 
                   ) 
             ) 
        ); 
+1

什麼是**精確**錯誤信息,你會得到什麼? – Rizier123

+0

閱讀[數組文檔](https://php.net/manual/en/language.types.array.php)。您正在使用不正確的語法。 –

回答

1

您必須刪除'['和']'。在這裏,你是正確的代碼

<?php 
$fake_data = array(
        "game_id" => "1", 
        "turn_number" => "1", 
        "host_user" => array(
              "units" => array(
                   array("row"=> "1", "col" => "1", "hp" => "100", "armor" => "100", "is_dead" => "0", "direction_facing" => "1", "name" => "AssaultAlpha"), 
                   array("row"=> "1", "col" => "2", "hp" => "100", "armor" => "100", "is_dead" => "0", "direction_facing" => "1", "name" => "JuggernautAlpha"), 
                   array("row"=> "1", "col" => "3", "hp" => "100", "armor" => "100", "is_dead" => "0", "direction_facing" => "1", "name" => "MedicAlpha") 

                  ), 
              "hit_indicators" => array(
                     array("row"=> "1", "col" => "3", "direction" => "1"), 
                     array("row"=> "1", "col" => "2", "direction" => "1") 
                   ) 
             ), 
        "client_user" => array(
              "units" => array(
                   array("row"=> "5", "col" => "1", "hp" => "100", "armor" => "100", "is_dead" => "0", "direction_facing" => "1", "name" => "AssaultAlpha"), 
                   array("row"=> "6", "col" => "2", "hp" => "100", "armor" => "100", "is_dead" => "0", "direction_facing" => "1", "name" => "JuggernautAlpha"), 
                   array("row"=> "7", "col" => "3", "hp" => "100", "armor" => "100", "is_dead" => "0", "direction_facing" => "1", "name" => "MedicAlpha") 

                  ), 
              "hit_indicators" => array(
                     array("row"=> "5", "col" => "3", "direction" => "1"), 
                     array("row"=> "6", "col" => "2", "direction" => "1") 
                   ) 
             ) 
        ); 
?> 
<pre><?php print_r($fake_data); ?></pre> 
+0

此外,如果您使用「,但不是」索引名稱,它會很好 - –

+0

廢話。您可以使用任何你想要的。 –

+0

@SverriM.Olsen使用'[「host_user」]'作爲數組鍵是非法。 –