PHP遍历问题

DEFAULT
  • 0
新手上路,请多包涵

请问php怎么才能把如下格式

{
    "code": 200,
    "message": "操作成功",
    "data": [
        {
            "id": 1,
            "goods_id": 1,
            "property_name_id": 1,
            "property_value_id": 1,
            "property_name": {
                "title": "份量",
                "is_sale": true
            },
            "property_value": {
                "id": 1,
                "value": "小份",
                "image": ""
            }
        },
        {
            "id": 2,
            "goods_id": 1,
            "property_name_id": 1,
            "property_value_id": 2,
            "property_name": {
                "title": "份量",
                "is_sale": true
            },
            "property_value": {
                "id": 2,
                "value": "中份",
                "image": ""
            }
        },
        {
            "id": 3,
            "goods_id": 1,
            "property_name_id": 2,
            "property_value_id": 4,
            "property_name": {
                "title": "温度",
                "is_sale": true
            },
            "property_value": {
                "id": 4,
                "value": "常温",
                "image": ""
            }
        },
        {
            "id": 4,
            "goods_id": 1,
            "property_name_id": 2,
            "property_value_id": 5,
            "property_name": {
                "title": "温度",
                "is_sale": true
            },
            "property_value": {
                "id": 5,
                "value": "加冰",
                "image": ""
            }
        }
    ]
}

遍历成如下格式

{
    "code": 200,
    "message": "操作成功",
    "data": [
        {
            "property_id": 1,
            "property_name": "份量",
            "is_sale": true,
            "items": [
                {
                    "id": 1,
                    "value": "小份",
                    "image": ""
                },
                {
                    "id": 2,
                    "value": "中份",
                    "image": ""
                }
            ]
        },
        {
            "property_id": 2,
            "property_name": "温度",
            "is_sale": true,
            "items": [
                {
                    "id": 4,
                    "value": "常温",
                    "image": ""
                },
                {
                    "id": 5,
                    "value": "加冰",
                    "image": ""
                }
            ]
        }
    ]
}
回复
阅读 401
2 个回答
<?php
$json = '{"code":200,"message":"操作成功","data":[{"id":1,"goods_id":1,"property_name_id":1,"property_value_id":1,"property_name":{"title":"份量","is_sale":true},"property_value":{"id":1,"value":"小份","image":""}},{"id":2,"goods_id":1,"property_name_id":1,"property_value_id":2,"property_name":{"title":"份量","is_sale":true},"property_value":{"id":2,"value":"中份","image":""}},{"id":3,"goods_id":1,"property_name_id":2,"property_value_id":4,"property_name":{"title":"温度","is_sale":true},"property_value":{"id":4,"value":"常温","image":""}},{"id":4,"goods_id":1,"property_name_id":2,"property_value_id":5,"property_name":{"title":"温度","is_sale":true},"property_value":{"id":5,"value":"加冰","image":""}}]}';
$arr = json_decode($json, true);
// 自己加逻辑判断保证key都存在
$newArr = [];
$newArr['code'] = $arr['code'];
$newArr['message'] = $arr['message'];

foreach ($arr['data'] as $v) {
    $id = $v['property_name_id'];

    if (!isset($newArr['data'][$id]['property_id']) && $newArr['data'][$id]['property_id'] != $v['property_value']['id']) {
        $newArr['data'][$id] = [
            'property_id' => $id,
            'property_name' => $v['property_name']['title'],
            'is_sale' => $v['property_name']['is_sale'],
        ];
    }

    $newArr['data'][$id]['item'][] = [
        'id' => $v['property_value']['id'],
        'value' => $v['property_value']['value'],
        'image' => $v['property_value']['image'],
    ];
}

$newArr['data'] = array_values($newArr['data']);
var_dump($newArr);
die;

节点关系变化多数据更新少,用匿名类解决比较方便。

<?php
$json_str = '{"code":200,"message":"操作成功","data":[{"id":1,"goods_id":1,"property_name_id":1,"property_value_id":1,"property_name":{"title":"份量","is_sale":true},"property_value":{"id":1,"value":"小份","image":""}},{"id":2,"goods_id":1,"property_name_id":1,"property_value_id":2,"property_name":{"title":"份量","is_sale":true},"property_value":{"id":2,"value":"中份","image":""}},{"id":3,"goods_id":1,"property_name_id":2,"property_value_id":4,"property_name":{"title":"温度","is_sale":true},"property_value":{"id":4,"value":"常温","image":""}},{"id":4,"goods_id":1,"property_name_id":2,"property_value_id":5,"property_name":{"title":"温度","is_sale":true},"property_value":{"id":5,"value":"加冰","image":""}}]}';

$data = [];
$obj = json_decode($json_str);

foreach ($obj->data as $v) {
    $nid = $v->property_name_id;
    if (!array_key_exists($nid, $data)) {
        $o = new Class {};
        $o->property_id = $v->property_name_id;
        $o->property_name = $v->property_name->title;
        $o->is_sale = $v->property_name->is_sale;
        $data[$nid] = $o;
    }
    $data[$nid]->items[] = $v->property_value;
}
// 重建索引
$obj->data = array_values($data);
echo json_encode($obj);
你知道吗?

宣传栏