如何根据需要转换json数据?

时间:2017-02-14 06:39:49

标签: php json

我有一个json数据,需要根据我的要求使用php进行转换。请帮我根据我的要求转换json数据。我想要 使用键号和food_type序列化数组。如果数字相同然后放入相同的数组,如果food_type相同,那么也将它们的内容放在food_content数组上。

这是我的代码:

 [{
"number": "1",
"food_type": "early_morning_breakfast",
"food_id": "839",
"name": "Rotisserie Chicken",
"food_serving": "1",
"calories": "322",
"food_serving_unit": "Piece",
"recipe_id": "1",
"recipe_type": "Veg"
}, {
"number": "1",
"food_type": "early_morning_breakfast",
"food_id": "18",
"name": "Prawn Biryani",
"food_serving": "1",
"calories": "125",
"food_serving_unit": "Bowl",
"recipe_id": "3",
"recipe_type": "Non-Veg"
 }, {
"number": "2",
"food_type": "breakfast",
"food_id": "6",
"name": "Aloo Shimla Mirch",
"food_serving": "1",
"calories": "225",
"food_serving_unit": "Bowl",
"recipe_id": "",
"recipe_type": ""
}, {
"number": "3",
"food_type": "evening_snacks",
"food_id": "5",
"name": "Aloo Methi",
"food_serving": "1",
"calories": "167",
"food_serving_unit": "Bowl",
"recipe_id": "",
"recipe_type": ""
 }]

我需要像这样转换:

[{
"name": "1",
"data": [{
    "food_type": "early_morning_breakfast",
    "food_content": [{
        "food_id": "839",
        "name": "Rotisserie Chicken",
        "food_serving": "1",
        "calories": "322",
        "food_serving_unit": "Piece",
        "recipe_id": "1",
        "recipe_type": "Veg"
    }, {
        "food_id": "18",
        "name": "Prawn Biryani",
        "food_serving": "1",
        "calories": "125",
        "food_serving_unit": "Bowl",
        "recipe_id": "3",
        "recipe_type": "Non-Veg"
    }]
    }]
 }, {
"name": "2",
"data": [{
    "food_type": "breakfast",
    "food_content": [{
        "food_id": "6",
        "name": "Aloo Shimla Mirch",
        "food_serving": "1",
        "calories": "225",
        "food_serving_unit": "Bowl",
        "recipe_id": "",
        "recipe_type": ""
    }]
   }]
  }, {
"name": "3",
"data": [{
    "food_type": "evening_snacks",
    "food_content": [{
        "food_id": "5",
        "name": "Aloo Methi",
        "food_serving": "1",
        "calories": "167",
        "food_serving_unit": "Bowl",
        "recipe_id": "",
        "recipe_type": ""
    }]
    }]
    }]

0 个答案:

没有答案