简体   繁体   English

需要帮助的一部分与json / php

[英]Need assistance for a part with json/php

I need help for generate the series data part into json 我需要帮助将序列数据部分生成json

var chart = new Highcharts.Chart({
    chart: {
        renderTo: 'container'
    },
    xAxis: {
        type: 'datetime'
    },
    series: [{
        data: [
            [Date.UTC(2010, 0, 1), 29.9],
            [Date.UTC(2010, 0, 2), 71.5],
            [Date.UTC(2010, 0, 3), 106.4],
            [Date.UTC(2010, 0, 6), 129.2],
            [Date.UTC(2010, 0, 7), 144.0],
            [Date.UTC(2010, 0, 8), 176.0]
         ]
    }]
});

My php part 我的PHP部分

foreach ($behaviour as $value) {
    $chart['xAxis']['categories'][]= time($value['date']);
}

Json output 杰森输出

[1318354710,1318354710,1318354710,1318354710]

I don't know what I need to do with my php code to do this part with a valid json. 我不知道我需要用我的php代码用有效的json做这部分。

part where I need help 我需要帮助的部分

series: [{
    data: [
        [Date.UTC(2010, 0, 1), 29.9],
        [Date.UTC(2010, 0, 2), 71.5],
        [Date.UTC(2010, 0, 3), 106.4],
        [Date.UTC(2010, 0, 6), 129.2],
        [Date.UTC(2010, 0, 7), 144.0],
        [Date.UTC(2010, 0, 8), 176.0]
     ]
}]

How can I solve this? 我该如何解决?

Date.UTC returns the number of milliseconds in a date string since midnight of January 1, 1970. This is the same as a UNIX timestamp. Date.UTC返回自1970年1月1日午夜以来的日期字符串中的毫秒数。这与UNIX时间戳相同。 So [Date.UTC(2010, 0, 1), 29.9] translates to [1262304000, 29.9] . 因此[Date.UTC(2010, 0, 1), 29.9]转换为[1262304000, 29.9]

There are a number of ways to get this timestamp in PHP. 有很多方法可以在PHP中获得此时间戳。 One simple (and object oriented) way is to use the DateTime class: 一种简单的(面向对象的)方法是使用DateTime类:

$time = new DateTime($value['date'], new DateTimezone('UTC'));
$chart['xAxis']['categories'][] = $time->getTimestamp();

Update 更新资料

Without knowing the ins and outs of your code, the best I can do is offer some sample code: 不知道您代码的来龙去脉,我能做的最好的就是提供一些示例代码:

<?php
// sample data
$behaviour = array(
    array(
        'date' => '2010-01-01',
        'datum' => 29.9
    ),
    array(
        'date' => '2010-01-02',
        'datum' => 71.5
    ),
    array(
        'date' => '2010-01-03',
        'datum' => 106.4
    ),
    array(
        'date' => '2010-01-06',
        'datum' => 129.2
    ),
    array(
        'date' => '2010-01-07',
        'datum' => 144
    ),
    array(
        'date' => '2010-01-08',
        'datum' => 176
    )
);

// creates the container information
$output = array(
    'chart' => array(
        'renderTo' => 'container'
    ),

    'xAxis' => array(
        'type' => 'datetime'
    ),

    'series' => array()
);

// adds the sample data
foreach ($behaviour as $value) {
    $time = new DateTime($value['date'], new DateTimezone('UTC'));
    $output['series'][0]['data'][] = array(
        $time->getTimestamp(), $value['datum']
    );
}

// output as JSON
header('Content-type: application/json');
echo json_encode($output); 
?>

Feel free to adapt it to your actual code. 随时根据您的实际代码进行调整。

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

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