views:

47

answers:

3

Hi guys, I'm having trouble trying to build an array in PHP which will output in the JSON format I am looking for. I will show you what I am trying to achieve and where I have got to so far:

[
    {"data":[{"x":3,"y":0},{"x":10,"y":0}]},
    {"data":[{"x":11,"y":0},{"x":13,"y":0}]},
    {"data":[{"x":12,"y":1},{"x":17,"y":1}]}
]

I am looping through db results and trying to build arrays to output the above json, my php looks like this (which is obviously not right yet):

//build the data
                    $data = array(
                                array(
                                    'x' => $age_start, 
                                    'y' => $ill_type
                                ),
                                array(
                                    'x' => $age_end, 
                                    'y' => $ill_type
                                )
                            );

                    $illnesses[] = $data;  

This code outputs the following json:

[
   {
     [
        [{"x":2,"y":6},{"x":2,"y":6}],
        [{"x":2,"y":6},{"x":5,"y":6}],
        [{"x":4,"y":6},{"x":4,"y":6}]
    ]
  }
]

Any pointers on this would be great!

+1  A: 

do this:

$data['data'] = array(
        array(
                'x' => $age_start,
                'y' => $ill_type
        ),
        array(
                'x' => $age_end,
                'y' => $ill_type
        )
);
ITroubs
+1  A: 

Basically, if you know your desired JSON output already, you can simply json_decode it to get it's representation in PHP. The var_export function prints the structure in parseable format. You can also use print_r or var_dump to dump the structure though.

$json = <<< JSON
[
    {"data":[{"x":3,"y":0},{"x":10,"y":0}]},
    {"data":[{"x":11,"y":0},{"x":13,"y":0}]},
    {"data":[{"x":12,"y":1},{"x":17,"y":1}]}
]
JSON;

var_export( json_decode($json) );

The above approach is universal. Just decode and dump the structure. Then assemble your code to create this structure and encode.

Gordon
Wooosh. Went completely over my head =(
Russell Dias
i agree with russell
ITroubs
Yes - the `json_decode` will generate the correct PHP structure :) I like it
gnud
Ah ok, that's very handy to know - thanks
spinozf
+1  A: 

Looking at the JSON string, you can see that:

  • it is an array (it is surrounded by [ and ])
  • each element is an object (surrounded by { and })
  • the objects have an element data that is itself an array
  • that array consists of two objects with an x and a y property

It is important to know that a JSON object is represented in PHP by an associative array (when json_encode()'ing, json_decode() has a specific parameter to use either a stdClass or an assoc. array).
So the php structure looks like this:

$data = array(
  array('data' => array(array('x' => 3, 'y' => 0), array('x' => 10, 'y' => 0))
  ,array('data' => array(array('x' => 11, 'y' => 0), array('x' => 13, 'y' => 0))
  ,array('data' => array(array('x' => 12, 'y' => 1), array('x' => 17, 'y' => 1))
);
Dennis Haarbrink
JSON objects are only represented as associative arrays if you put the second argument to `json_decode` to TRUE. Otherwise they are StdClass objects. What you mean is, associative arrays in PHP are encoded to JSON objects. Apart from that tiny nitpick, +1.
Gordon
@Gordon: You are absolutely right, I updated my answer.
Dennis Haarbrink