如何扁平化JSON输出PHP数组

时间:2019-04-21 19:55:27

标签: php arrays json nested flatten

我几乎是在自学代码,并在创建刮板应用程序方面发挥了一些作用。我在json API中使用了一个数组,但是我需要弄平结构,以便可以在其他地方轻松使用它。

我真的很想找出我在这里需要做的事情。我已经阅读了许多其他类似请求的问题,但是数组格式从未与我的格式匹配,所以我实际上不知道如何解决这个问题。

这是当前的数组结构

{
    "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"
            }
        }
    ]
}

我想实现这一目标

{
    "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"
        }
    ]
}

这是我用来生成数组的代码,然后将其转换为json以在api中使用。我正在遍历对数据库的先前调用,以将重复的记录合并在一起,但保留唯一的日期和价格数据。所以也许有一种方法可以更改此代码以获得我想要的输出?

    $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);

任何帮助表示赞赏!

2 个答案:

答案 0 :(得分:2)

喜欢:

$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);

在原始JSON字符串中, result 键的第一级是具有单个索引项的数组,该数组将一旦解码为多维数组,就将索引0设置为零。级别的索引,您只需要使用array_values PHP函数。

答案 1 :(得分:0)

这应该有效

$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);