I have a json data and needs to convert according to my requirement using php. Please help me to convert json data to according to my requirement. I want to serialize array using key number and food_type. If the number is same then put into the same array, also if food_type same then also put their content on food_content array.
Here is the my code:
[{
"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": ""
}]
I need to convert like this:
[{
"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": ""
}]
}]
}]