-2

I come from a javascript background, I want to do something similar to this.

$questions = [
      {
       "title" => "this is the title",
       "description" => "this is the desctiption"
      },
      {
       "title" => "this is the title2",
       "description" => "this is the desctiption2"
      }
    ];

How do I create an array of objects in PHP?

3
  • I think associative arrays is probably the best starting point but if you want true PHP objects then you will have to look into stdClass() Commented May 29, 2018 at 17:00
  • Create objects and put in a array ? What have you try to do by the way ? basically, with your exemple, create a class with your attributes in a constructor, instanciate them dirrectly in the array Commented May 29, 2018 at 17:00
  • 1
    Basically, change those curly brackets { into square brackets [ Commented May 29, 2018 at 17:01

3 Answers 3

3

The quickest way with a standard object is to create arrays and cast to an object:

$questions = [
      (object)[
       "title" => "this is the title",
       "description" => "this is the desctiption"
      ],
      (object)[
       "title" => "this is the title2",
       "description" => "this is the desctiption2"
      ]
];

Or you can JSON encode and decode an array:

$questions = [
      [
       "title" => "this is the title",
       "description" => "this is the desctiption"
      ],
      [
       "title" => "this is the title2",
       "description" => "this is the desctiption2"
      ]
];

$questions = json_decode(json_encode($questions));

If you're doing this for the purpose of using it in JSON, then just build an array. Arrays with string keys will be objects when encoded.

Sign up to request clarification or add additional context in comments.

Comments

2

Your example appears to be a properly formed JS array/object declaration:

var questions = [
  {
   "title" => "this is the title",
   "description" => "this is the desctiption"
  },
  {
   "title" => "this is the title2",
   "description" => "this is the desctiption2"
  }
];

So the easiest way to achieve a similar result in PHP is:

$questions = [
    [
        "title" => "this is the title",
        "description" => "this is the desctiption"
    ],
    [
        "title" => "this is the title2",
        "description" => "this is the desctiption2"
    ]
];

2 Comments

That's an array of arrays, not objects.
@AbraCadaver You're right, hopefully my update corrects any misconception.
0

This is not real a "true" object since there are only properties. This kind of structure will be best handled as simple arrays, and your JS string can easily be converted to an array like this :

$questions = '[
    {
    "title" => "this is the title",
    "description" => "this is the desctiption"
    },
    {
    "title" => "this is the title2",
    "description" => "this is the desctiption2"
    }
]';

$my_array = json_decode($questions, true);

Note that the true argument of json_decode will force the output to be an associative array. Then your $my_array will be:

array(2)
{
    [0]=>
    array(2) {
        ["title"]=>
        string(17) "this is the title"
        ["description"]=>
        string(23) "this is the desctiption"
    }
    [1]=>
    array(2) {
        ["title"]=>
        string(18) "this is the title2"
        ["description"]=>
        string(24) "this is the desctiption2"
    }
}

Comments

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.