Reputation: 950
This is a bit of a reverse engineering question, but I want to know how to write in PHP a proper multidimensional array in PHP that outputs the following javascript array.
[
{
"key": "Basic Planners",
"values": [{"x": "YourPhone","y": 150},
{"x": "Universe X3","y": 300},
{"x": "ePhone 74s","y": 1500},
{"x": "NextUs","y": 50},
{"x": "Humanoid","y": 500
}]
}, {
"key": "No-Namers",
"values": [{"x": "YourPhone","y": 300},
{"x": "Universe X3","y": 250},
{"x": "ePhone 74s","y": 400},
{"x": "NextUs","y": 150},
{"x": "Humanoid","y": 900}]
}, {
"key": "Feature Followers",
"values": [{"x": "YourPhone","y": 350},
{"x": "Universe X3","y": 900},
{"x": "ePhone 74s","y": 100},
{"x": "NextUs","y": 500},
{"x": "Humanoid","y": 250}]
}, {
"key": "Hipsters & Elites",
"values": [{"x": "YourPhone","y": 200},
{"x": "Universe X3","y": 350},
{"x": "ePhone 74s","y": 50},
{"x": "NextUs","y": 800},
{"x": "Humanoid","y": 100}]
}
]
Upvotes: 0
Views: 86
Reputation: 38
The following code should do the trick
$phpArray = array(
array(
'key' => 'Basic Planners',
'values'=> array(
array('x' => 'YourPhone', 'y' => 150),
array('x' => 'Universe X3', 'y' => 300),
array('x' => 'ePhone 74s', 'y' => 1500),
array('x' => 'NextUs', 'y' => 50),
array('x' => 'Humanoid', 'y' => 500),
)
),
/* and so on... */
);
echo json_encode($phpArray);
Upvotes: 1
Reputation: 4052
For the JSON Objects use array("key" => value, ...)
for the JSON Arrays use array(arg0, arg1, arg2, ...)
Then just nest these various groupings. This should output the desired JSON.
Upvotes: 0