简体   繁体   中英

Generate JSON for Google Charts using PHP

I'm following the guide at: https://developers.google.com/chart/interactive/docs/php_example

They give a json snippet for a json example. How do yo build this one with php? Usually there is just converting arrays to json using json_encode() but this time it seems like you need an object aswell. Can anyone clarify this?

The json snippet:

{
  "cols": [
    {"id":"","label":"Topping","pattern":"","type":"string"},
    {"id":"","label":"Slices","pattern":"","type":"number"}
  ],
  "rows": [
    {"c":[{"v":"Mushrooms","f":null},{"v":3,"f":null}]},
    {"c":[{"v":"Onions","f":null},{"v":1,"f":null}]},
    {"c":[{"v":"Olives","f":null},{"v":1,"f":null}]},
    {"c":[{"v":"Zucchini","f":null},{"v":1,"f":null}]},
    {"c":[{"v":"Pepperoni","f":null},{"v":2,"f":null}]}
  ]
}

What I have so far:

$obj = new stdClass();
$obj->cols= array(
    array("id"=>"", "label"=>"Topping", "pattern"=>"", "type"=>"string"), 
    array("id"=>"", "label"=>"Slices", "pattern"=>"", "type"=>"string"));
$obj->rows = array(
    array()
);

echo json_encode($obj);

Is there anyone that knows how to complete this php representation?

Edit: My echo outputs:

{"cols":[{"id":"","label":"Topping","pattern":"","type":"string"},{"id":"","label":"Slices","pattern":"","type":"string"}],"rows":[[]]}

PHP associative arrays will converts to objects in JSON, so stdClass is not needed. You already got 80% of the structure so here are a few pointers:

$data = [
    'cols' => [],
    'rows' => [],
];

will result in :

{
    'cols': [],
    'rows': [],
}

In order to get JSON arrays, don't give keys to the values:

$data = [
    'c' => [
        [ // <- no key here
            'v' => 'Mushroom', 
            'f' => null
        ],
        [ // <- no key here
            'v' => '3', 
            'f' => null
        ],
    ],
    // ...
];

will give you a data row:

{
    "c": [ // <- we got an actual array here because there was no key
        {
            "v":"Mushrooms",
            "f":null
        },
        {
            "v":3,
            "f":null
        }
    ]
}

This code should work:

<?php
$obj = array("cols"=>array(
    array("id"=>"", "label"=>"Topping", "pattern"=>"", "type"=>"string"), 
    array("id"=>"", "label"=>"Slices", "pattern"=>"", "type"=>"string")),
    "rows"=>array(
    array("c"=>array(array("v"=>"Mushrooms", "f"=>null), array("v"=>3, "f"=>null))),
    array("c"=>array(array("v"=>"Onions", "f"=>null), array("v"=>1, "f"=>null))),
    array("c"=>array(array("v"=>"Olives", "f"=>null), array("v"=>1, "f"=>null))),
    array("c"=>array(array("v"=>"Zucchini", "f"=>null), array("v"=>1, "f"=>null))),
    array("c"=>array(array("v"=>"Pepperoni", "f"=>null), array("v"=>2, "f"=>null))),
    )
);

echo json_encode($obj);
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM