创建数组

时间:2015-04-26 08:47:23

标签: php arrays laravel

我试图在PHP中以数组的形式制作一些虚假数据并在运行此行时不断收到非法偏移类型错误,是否有人可以帮助解释原因?

我查找了非法偏移错误的原因,并且看起来我没有通过偏移访问任何数据,我正在尝试创建一个数组,而且我在哪里看不到我正在使用的地方object作为数组的关联。

$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 个答案:

答案 0 :(得分: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>