Ted Scheckler
Ted Scheckler

Reputation: 1511

JSON object in PHP with property

What would be the equivalent PHP array structure to create an object with identical properties:

For example... create the object 'columns' below in PHP using json_encode:

jQuery('#example').dataTable( {
  "ajaxSource": "sources/objects.txt",
  "columns": [
    { "data": "engine" },
    { "data": "browser" },
    { "data": "platform" },
    { "data": "version" },
    { "data": "grade" }
  ]
} );

(I am trying to build a dynamic datatable and define the columns in the source JSON.

Upvotes: 1

Views: 224

Answers (1)

Alexis Peters
Alexis Peters

Reputation: 1621

You could use an ArrayObject

new ArrayObject([
 "ajaxSource" => "...",
  "columns" => [
    new ArrayObject(['data' => 'engine']),
    new ArrayObject(['data' => 'browser']),
    new ArrayObject(['data' => 'etc'])
  ]
]);

if you want to assemble this you need to store the objects inside an array like

$columns = [];
for(...) {
$columns[] = new ArrayObject(['data' => 'etc']);
}

Have a look at http://php.net/manual/de/arrayobject.construct.php

Upvotes: 1

Related Questions