简体   繁体   English

PHP输出JSON文件

[英]PHP Outputing JSON file

I am trying to achieve the following JSON format: 我正在尝试实现以下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
                },
            ]
        }]

so that i could later call it asynchronously like this: 这样我以后可以像这样异步调用它:

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

This is my attempt to generate the desired JSON 这是我尝试生成所需的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);
}

and this is the result 这就是结果

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

I am confused as to how to do this part: 我对如何执行此部分感到困惑:

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

Could anyone tell me how to correct this? 谁能告诉我该如何解决?

The inner array belongs to data . 内部数组属于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);
}

You just had your array out the wrong way, try this: 您只是将阵列弄错了,请尝试以下操作:

$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'
            )
        ),
    )
);

That will give you what you want. 那会给你你想要的。


Explanation 说明

This array: 该数组:

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

belongs within the data array :) 属于data数组:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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