0

I am trying to make some fake data in the form of an array in PHP and keep getting an Illegal Offset Type error when running this line, could anyone help explain why?

I looked up reasons for illegal offset errors and it doesn't seem like I am accessing any data via an offset, I am attempting to create an array, and no where do I see a spot where I am using an object as the association for the array.

$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")
                                                                )
                                          )
                    );
2
  • 1
    What is the exact error message which you get? Commented Apr 26, 2015 at 8:49
  • Read the documentation for arrays. You are using an incorrect syntax. Commented Apr 26, 2015 at 9:02

1 Answer 1

3

You have to remove the '[' and ']'. Here you are the corrected code

<?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>
Sign up to request clarification or add additional context in comments.

5 Comments

Also it is good if you use ', but not " for index names - it talks for good style.
Nonsense. You can use whichever you want.
@SverriM.Olsen using ["host_user"] as array key is illegal.
@u_mulder I never said it wasn't. I was commenting on the use of ' vs ". It is a matter of taste, and sometimes function, and has nothing to do with "good style".
OK, ok, the comment about ' and " was main in not directly related to the question. It seems I'm in mistake for this ( ' / " ) and @SverriM.Olsen is quite right. Any way the answer above is working

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.