我有一个json api提要,我想对其进行重组以用于Google折线图中,但是我不知道如何遍历json数据以在php中创建一个新数组。为了使事情复杂化,我要为每个项目绘制的数据位于不同的行上。
这是json数据
gitPullAll() {
for branch in `git branch | sed -E 's/^\*/ /' | awk '{print $1}'`; do
git checkout $branch
git pull -p
printf "\n"
done
echo "Done"
}
这是我要为折线图实现的结构...
{
"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"
}
]
}
我真的不知道从哪里开始。我发现的其他问题/解决方案没有相同的原始数据格式。任何帮助表示赞赏!
答案 0 :(得分:0)
您可以在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
将包含您的需求格式