我有一个json api提要,我想对其进行重组以在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" },我真的不知道从哪里开始。我发现的其他问题/解决方案没有相同的原始数据格式。任何帮助表示赞赏!
1 回答
忽然笑
TA贡献1806条经验 获得超5个赞
您可以在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将包含你的愿望格式
- 1 回答
- 0 关注
- 114 浏览
添加回答
举报
0/150
提交
取消