<?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": ""
}
}
]
}';
$obj = json_decode($json_str);
$data = [];
foreach($obj->data as $v){
$nid=$v->property_name_id;
if(!array_key_exists($nid,$data)){
$o = new stdclass;
$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);
print_r($obj);
//echo json_encode($obj);
preferences:
47.59 ms | 402 KiB | 5 Q