Hi I am trying to group a set of arrays from a JSON by its key.
The JSON I have looks like this:
{
"Borges Group": [
{
"id": 936,
"name": "Borges 2",
"price": 9.99,
"dsc": "a desc of the item",
"options": [
{
"Drink 1": [
{
"name": "Fanta"
},
{
"name": "7UP"
},
{
"name": "Water"
}
]
},
{
"Drink 2": [
{
"name": "Fanta"
},
{
"name": "7UP"
},
{
"name": "Water"
}
]
},
{
"Side": [ { "name": "Pasta" }, { "name": "Rice" } ]
}
]
}
]
}
So I iterate through it with PHP like this, I am only including the options part:
foreach ($product->options as $var) {
foreach ($var as $b){
foreach ($b as $c){
echo $c->name."<br>";
}
}
}
And this gives me:
Fanta
7UP
Water
Fanta
7UP
Water
Pasta
Rice
Which is fine because it is supposed to, I wanted to group it so that it shows something like this instead:
Drink 1 - Fanta, 7UP, Water
Drink 2 - Fanta, 7UP, Water
Side - Pasta, Rice
I want to create a dropdown option for each of the arrays (Drink 1, Drink 2 and Side) later on, hence the need to group them. What might be a good way to achieve this? Please feel free to suggest improvements on the code too. Cheers!
No responses yet.