0

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}]
        }
      ]
2
  • It's a little amusing that instead of having a key called "Basic Planners" you have a key called "key" with a value of "Basic Planners". It would make the structure a lot more compact to use {"Basic Planners": [{"x":...},...], "Feature Followers":[...] etc Commented Jul 5, 2014 at 2:47
  • Good point. I was using the format for an nvd3.js graph, tested without that key statement and it works so I'll drop that from the php (data coming in that way). Commented Jul 5, 2014 at 2:51

2 Answers 2

1

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

Thanks dude! I kept getting close but screwing up. I'm more of a designer, CSS and jQuery guy than server side.
0

PHP Manual

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.

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.