开发者

Help trying to generate tricky multidimensional array format

开发者 https://www.devze.com 2023-01-19 05:17 出处:网络
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:

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 lo开发者_StackOverflowoks 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!


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.


do this:

$data['data'] = array(
        array(
                'x' => $age_start,
                'y' => $ill_type
        ),
        array(
                'x' => $age_end,
                'y' => $ill_type
        )
);


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))
);
0

精彩评论

暂无评论...
验证码 换一张
取 消