0

我试图建立一个非常简单的表:

Sex  %
M    40
F    60

使用 php 对数组进行编码,但假设输入数据错误,则不绘制图形。你能说出问题出在哪里吗?

$table = array();
$table['cols'] = array(
array('label' => 'Sesso', 'type' => 'string'),
array('label' => 'Quantita', 'type' => 'number'));

$rows = array();
$temp = array();
$temp[] = array('v' => 'M'); 
$temp[] = array('v' => 60); 
$rows[] = array('c' => $temp);
$temp = array();
$temp[] = array('v' => 'F'); 
$temp[] = array('v' => 40); 
$rows[] = array('c' => $temp);

$table['rows'] = $rows;
$jsonTable = json_encode($table);
4

2 回答 2

1

如果您尝试:

$table = array(
   'cols' => array(
      array(
         'id' => '1',
         'label' => 'Sesso',
         'type' => 'string'
      ),
      array(
         'id' => '2',
         'label' => 'Quantita',
         'type' => 'number'
      )
   ),
   'rows' => array(
      array(
         'c' => array(
            array(
               'v' => 'M',
               'f' => 60
            )
         )
      ),
      array(
         'c' => array(
            array(
               'v' => 'F',
               'f' => 40
            )
         )
      )
   )
);

$json = json_encode($table);

在他们的文档中查看 Google 的示例。您应该针对的 JSON 结构是:

{
  "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}]}
      ]
}
于 2013-01-19T17:13:42.560 回答
0

这就是我所做的:

    $table = array(
        'cols' => array(
            array( 'id' => '', 'label' => 'Sesso', 'pattern' => '', 'type' => 'string' ),
            array( 'id' => '', 'label' => '#', 'pattern' => '', 'type' => 'number' )
        ),
        'rows' => array(
            array( 'c' => array( array( 'v' => 'Uomini', 'f' => null ), array('v' => 60, 'f' => null) ) ),
            array( 'c' => array( array( 'v' => 'Donne', 'f' => null ), array('v' => 40, 'f' => null) ) )
        )
    );
于 2013-01-19T23:12:55.340 回答