I'm pretty much self teaching myself to code and having a little play with creating a scraper app. I have an array I'm using in an json api but I need to flatten the structure so I can easily consume it elsewhere.
I'm really struggling to fathom out what I need to do here. I've read a load of other questions which are similar requests, but the array formats never match my format so I don't actually know how to tackle this.
This is the current array structure
{
"result": [
{
"Boot buddy": {
"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,2019-04-14 21:50:31.362,2019-04-14 21:54:11.184",
"price": "£14.99,£14.99,£14.99"
},
"Amazon echo": {
"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,2019-04-14 21:50:31.362,2019-04-14 21:54:11.184",
"price": "£14.99,£14.99,£14.99"
}
}
]
}
I want to achieve this
{
"result": [
{
"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,2019-04-14 21:50:31.362,2019-04-14 21:54:11.184",
"price": "£14.99,£14.99,£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,2019-04-14 21:50:31.362,2019-04-14 21:54:11.184",
"price": "£14.99,£14.99,£14.99"
}
]
}
This is the code I'm using to generate the array which then gets converted into json for use in the api. I'm iterating over a previous call to the database to merge duplicate records together but keep the unique date and price data. So maybe there's a way to change this code to get the output I'm after?
$records=array();
$records[result]=array();
foreach ($products_arr[records] as $key => $value) {
$hash = $value['name'];
if(isset($result[$hash])){
$result[$hash]['date'] .= ",{$value['date']}";
$result[$hash]['price'] .= ",{$value['price']}";
}else{
$result[$hash] = $value;
}
}
array_push($records[result], $result);
Any help appreciated!!
Parse error: syntax error, unexpected 'http_response_code' (T_STRING) in C:\xampp\htdocs\api\product\read-merged.php on line 79
– Chris Apr 21 '19 at 20:10