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}]
}
]
asked Jul 5, 2014 at 2:20
dvoutt
9602 gold badges9 silver badges24 bronze badges
2 Answers 2
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);
Sign up to request clarification or add additional context in comments.
1 Comment
dvoutt
Thanks dude! I kept getting close but screwing up. I'm more of a designer, CSS and jQuery guy than server side.
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.
answered Jul 5, 2014 at 2:36
Strikeskids
4,08215 silver badges27 bronze badges
Comments
default
{"Basic Planners": [{"x":...},...], "Feature Followers":[...]etc