Chris
Chris

Reputation: 105

How to flatten array for json output php

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!!

Upvotes: 0

Views: 349

Answers (2)

Danish Hakim Khan
Danish Hakim Khan

Reputation: 263

This should work

$json = '{
    "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"
            }
        }
    ]
}';

$arr = (array) json_decode($json)->result[0];
foreach($arr as $data){
  $newArr['result'][] = $data;
}
echo json_encode($newArr);

Upvotes: 0

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89567

Like that:

$json=<<<'EOD'
{
    "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"
            }
        }
    ]
}
EOD;

$arr = json_decode($json, true);
$arr['result'] = array_values($arr['result'][0]);
$result = json_encode($arr);

In the orginal JSON string, the first level of the result key is an array with a single indexed item that gives once decoded to a multidimensionnal array the index 0. To turn all keys contained at this level to indexes, you only need to use the array_values PHP function.

Upvotes: 2

Related Questions