如何更改用于谷歌折线图的数据结构

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何更改用于谷歌折线图的数据结构相关的知识,希望对你有一定的参考价值。

我有一个json api feed,我想重组用于google折线图,但我不知道如何循环json数据在php中创建一个新数组。更复杂的是,我想为每个项目绘制的数据位于不同的行上。

这是json数据

{
    "records": [
        {
            "id": "2",
            "groupno": "1",
            "urlsource": "https://www.amazon.co.uk/Boot-Buddy-fastest-simplest-footwear/dp/B014UPAHO4?pd_rd_wg=lVVK6&pd_rd_r=bf1ba871-fb59-4c66-a146-e94dde7c8e6d&pd_rd_w=gWC2F&ref_=pd_gw_ri&pf_rd_r=W68MX1TXFDDJ8Q8Z08CP&pf_rd_p=cecd4520-32f6-5499-ae19-cd4e83816acd",
            "name": "Boot buddy",
            "date": "2019-04-14 16:00:29.595",
            "price": "£14.99"
        },
        {
            "id": "1",
            "groupno": "1",
            "urlsource": "https://www.amazon.co.uk/dp/B07CH6JKW3/ref=gw_uk_desk_h1_aucc_cp_mp?pf_rd_p=e4e5a2e6-ddbd-473a-a5fb-e8cc09a11f88&pf_rd_r=1MN25BRXY8YDQ4TBK4X6",
            "name": "Amazon echo",
            "date": "2019-04-14 16:00:29.595",
            "price": "£19.99"
        },
        {
            "id": "2",
            "groupno": "1",
            "urlsource": "https://www.amazon.co.uk/Boot-Buddy-fastest-simplest-footwear/dp/B014UPAHO4?pd_rd_wg=lVVK6&pd_rd_r=bf1ba871-fb59-4c66-a146-e94dde7c8e6d&pd_rd_w=gWC2F&ref_=pd_gw_ri&pf_rd_r=W68MX1TXFDDJ8Q8Z08CP&pf_rd_p=cecd4520-32f6-5499-ae19-cd4e83816acd",
            "name": "Boot buddy",
            "date": "2019-04-14 21:50:31.362",
            "price": "£14.99"
        },
        {
            "id": "1",
            "groupno": "1",
            "urlsource": "https://www.amazon.co.uk/dp/B07CH6JKW3/ref=gw_uk_desk_h1_aucc_cp_mp?pf_rd_p=e4e5a2e6-ddbd-473a-a5fb-e8cc09a11f88&pf_rd_r=1MN25BRXY8YDQ4TBK4X6",
            "name": "Amazon echo",
            "date": "2019-04-14 21:50:31.362",
            "price": "£19.99"
        },
        {
            "id": "2",
            "groupno": "1",
            "urlsource": "https://www.amazon.co.uk/Boot-Buddy-fastest-simplest-footwear/dp/B014UPAHO4?pd_rd_wg=lVVK6&pd_rd_r=bf1ba871-fb59-4c66-a146-e94dde7c8e6d&pd_rd_w=gWC2F&ref_=pd_gw_ri&pf_rd_r=W68MX1TXFDDJ8Q8Z08CP&pf_rd_p=cecd4520-32f6-5499-ae19-cd4e83816acd",
            "name": "Boot buddy",
            "date": "2019-04-14 21:54:11.184",
            "price": "£12.99"
        },
        {
            "id": "1",
            "groupno": "1",
            "urlsource": "https://www.amazon.co.uk/dp/B07CH6JKW3/ref=gw_uk_desk_h1_aucc_cp_mp?pf_rd_p=e4e5a2e6-ddbd-473a-a5fb-e8cc09a11f88&pf_rd_r=1MN25BRXY8YDQ4TBK4X6",
            "name": "Amazon echo",
            "date": "2019-04-14 21:54:11.184",
            "price": "£17.99"
        }
    ]
}

这是我想要为线图实现的结构......

[
    ['Date time',                   'Boot buddy price',     'Amazon echo price'],
    ['2019-04-14 16:00:29.595',     £14.99,                 £19.99],
    ['2019-04-14 21:50:31.362',     £14.99,                 £19.99],
    ['2019-04-14 21:54:11.184',     £12.99,                 £17.99],
]

我字面上不知道从哪里开始。我发现的其他问题/解决方案没有相同的原始数据格式。任何帮助赞赏!

答案

你可以在你的json上循环两次 - 首先在数组上获取你想要的数据并格式化它们。

首先,在json上循环:

$arr = jsone_decode($jsonStr, true);
foreach($arr as $e) {
    $data[$e["date"]][$e["name"]] = $e["price"];
}

现在,再次在$data上循环以格式化输出:

$res[] = ['Date time', 'Boot buddy price', 'Amazon echo price']; // set the header
foreach($data as $k => $e) {
    $res[] = [$k, $e["Boot buddy"], $e["Amazon echo"]];
}

$res将包含您的愿望格式

以上是关于如何更改用于谷歌折线图的数据结构的主要内容,如果未能解决你的问题,请参考以下文章

如何修改谷歌折线图的JSON字符串

多线谷歌折线图,使用来自 mySQL db 的数据

我无法使用 mysql 表中的数据创建谷歌折线图

谷歌折线图显示负值

用单值显示谷歌折线图

谷歌折线图双Y轴问题