0

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":[[]]}
12
  • so what does your code echo as result? Commented Aug 2, 2016 at 9:37
  • 2
    PHP associative arrays will convert to JSON objects, you do not need stdClass Commented Aug 2, 2016 at 9:37
  • @JulianKuchlbauer, sorry. I've updated the question and inserted my output Commented Aug 2, 2016 at 9:39
  • @ndm, so what structure would you use on the arrays? Commented Aug 2, 2016 at 9:39
  • 1
    @NDM There is no reason why you would not use stdClass though if you wanted to make your code self documenting Commented Aug 2, 2016 at 9:41

2 Answers 2

2

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
        }
    ]
}
Sign up to request clarification or add additional context in comments.

3 Comments

Ahh, now I know what you talk about. I didn't know I could use arrays this way. I thought each {} was needed to be an object. Upvoted!
I think you will have to wrap null in quote like 'null' otherwise you get nothing in the output
no: if you wrap it in quotes you will get the string 'null' in your JSON and not the null constant.
0

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

1 Comment

I know, but he wanted a code, that just works for this specific problem and thats what i delivered. Have my point on your definitly more sophisticated answer.

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.