-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?

LeBlaireau
  • 17,133
  • 33
  • 112
  • 192
  • I think [associative arrays](http://php.net/manual/en/language.types.array.php) is probably the best starting point but if you want true PHP objects then you will have to look into [`stdClass()`](https://stackoverflow.com/q/1434368/2191572) – MonkeyZeus May 29 '18 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 – Frankich May 29 '18 at 17:00
  • 1
    Basically, change those curly brackets `{` into square brackets `[` – MonkeyZeus May 29 '18 at 17:01

3 Answers3

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.

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
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"
    ]
];
MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77
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"
    }
}
WizardNx
  • 697
  • 5
  • 10