用于flot chart的json_encode

时间:2015-07-25 19:47:35

标签: php json flot

我很难为flot图表生成特定格式的JSON。非常感谢任何帮助!

我使用这个PHP代码

$query = mysql_query('SELECT * FROM all_programs_extended');

while($row = mysql_fetch_assoc($query)) {
$newData[] = array(
                    'label' => $row['program_type'],
                    'data'  => array(
                "employee1" => $row['employee1'],
                                "employee2" => $row['employee2'],
                "employee3" => $row['employee3']                        
                                )
                    );

}


print json_encode($newData);

生成此有效JSON

[{"label":"Program A","data":{"employee1":"5","employee2":"3","employee3":"1"}},
{"label":"Program B","data":{"employee1":"0","employee2":"4","employee3":"2"}},
{"label":"Program A","data":{"employee1":"4","employee2":"2","employee3":"4"}}]

但我需要它看起来像这样:

{
    "label": "Program A",
    "data": [[employee1, 5], [employee2, 3], [employee3, 1]],   
     "label": "Program B",
    "data": [[employee1, 0], [employee2, 4], [employee3, 2]],
     "label": "Program C",
    "data": [[employee1, 4], [employee2, 2], [employee3, 4]]

}

我好像用括号和大括号混淆了,我还需要一个逗号分隔括号中的值对,而不是第一个JSON中的冒号。很高兴在数字周围加上引号!

非常感谢!

1 个答案:

答案 0 :(得分:1)

通过一个额外的数组来覆盖employeeX

$newData[] = array(
    'label' => $row['program_type'],
    'data'  => array(
        array(
            "employee1" => $row['employee1']
        ),
        array (
            "employee2" => $row['employee2']
        ),
        array (
            "employee3" => $row['employee3']
        )
    )
);