PHP输出JSON文件

时间:2014-06-24 05:52:12

标签: php json highcharts

我正在尝试实现以下JSON格式:

function generatePieTicket(data) {
$('#piecharts').highcharts({
.....
series: [{
            type: 'pie',
            name: 'Ticket Share by Team',
            data: [
                ['Technical', 45.0],
                ['Logistic', 26.8],
                {
                    name: 'Others',
                    y: 28.2,
                    sliced: true,
                    selected: true
                },
            ]
        }]

这样我以后可以像这样异步调用它:

function generatePieTicket(data) {
$('#piecharts').highcharts({
....
series: data

这是我尝试生成所需的JSON

function getPieData(){
    $data = array(
        array(
            'type' => 'pie',
            'name' => 'Sample Pie Chart',
            'data' => array(
                array('Technical', 45.0),
                array('Logistics', 26.8)
            ),
            '' => array(
                'name' => 'others',
                'y' => 28.2,
                'sliced' => 'true',
                'selected' => 'true'
            )
        )
    );

    header('Content-Type: application/json');
    return json_encode($data);
}

这是结果

[
    {
        "type": "pie",
        "name": "Sample Pie Chart",
        "data": [
            [
                "Technical",
                45
            ],
            [
                "Logistics",
                26.8
            ]
        ],
        "": {
            "name": "others",
            "y": 28.2,
            "sliced": "true",
            "selected": "true"
        }
    }
]

我对如何做这部分感到困惑:

data: [
    ['Technical', 45.0],
    ['Logistic', 26.8],
    {
        name: 'Others',
        y: 28.2,
        sliced: true,
        selected: true
    },
]

有谁能告诉我如何纠正这个问题?

2 个答案:

答案 0 :(得分:2)

内部数组属于data

function getPieData(){
    $data = array(
        array(
            'type' => 'pie',
            'name' => 'Sample Pie Chart',
            'data' => array(
                array('Technical', 45.0),
                array('Logistics', 26.8),
                array(
                    'name' => 'others',
                    'y' => 28.2,
                    'sliced' => 'true',
                    'selected' => 'true',
                ),
            )
        )
    );

    header('Content-Type: application/json');
    return json_encode($data);
}

答案 1 :(得分:1)

你的阵列出错了,试试这个:

$data = array(
    array(
        'type' => 'pie',
        'name' => 'Sample Pie Chart',
        'data' => array(
            array('Technical', 45.0),
            array('Logistics', 26.8),
            array(
                'name' => 'others',
                'y' => 28.2,
                'sliced' => 'true',
                'selected' => 'true'
            )
        ),
    )
);

那会给你你想要的东西。


<强>解释

这个数组:

array(
'name' => 'others',
 'y' => 28.2,
 'sliced' => 'true',
 'selected' => 'true'
)

属于data数组:)