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

Php 如何根据需要转换json数据?,php,json,Php,Json,我有一个json数据,需要使用php根据我的要求进行转换。请根据我的要求帮助我将json数据转换为。我想 使用键号和键类型序列化数组。如果数字相同,则放入同一数组;如果食物类型相同,则也将其内容放入食物内容数组 以下是我的代码: [{ "number": "1", "food_type": "early_morning_breakfast", "food_id": "839", "name": "Rotisserie Chicken", "food_serving": "1", "calori

我有一个json数据,需要使用php根据我的要求进行转换。请根据我的要求帮助我将json数据转换为。我想 使用键号和键类型序列化数组。如果数字相同,则放入同一数组;如果食物类型相同,则也将其内容放入食物内容数组

以下是我的代码:

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

json\u decode()
循环并构造新数组会有所帮助。但是你尝试过什么吗?是的,尝试过但没有得到同样的结果,你能帮助我们吗?告诉我们你尝试过什么?你做了什么来实现这一点?在此处显示您的代码/工作。StackOverflow不是代码编写服务。如果您的代码有问题,请提供最小、完整和可验证的示例。