我有一个包含食物项目的数据数组,并且看起来像这样。Laravel:循环访问一个数组并将其转换为带有原始数组的内部密钥
[
{
"itemId": 80001,
"name": "FRENCH FRIES SMALL",
"description": "More delicious than ever, our signature piping hot, thick cut Salted French Fries are golden on the outside and fluffy on the inside.",
"price": 6,
"slug": "french-fries-small-80001"
},
{
"itemId": 80002,
"name": "FRENCH FRIES MEDIUM",
"description": "More delicious than ever, our signature piping hot, thick cut Salted French Fries are golden on the outside and fluffy on the inside.",
"price": 7,
"slug": "french-fries-medium-80002"
},
{
"itemId": 80003,
"name": "FRENCH FRIES LARGE",
"description": "More delicious than ever, our signature piping hot, thick cut Salted French Fries are golden on the outside and fluffy on the inside.",
"price": 8,
"slug": "french-fries-large-80003"
},
{
"itemId": 80052,
"name": "CRINCKLE WEDGES SMALL",
"description": "CRINCKLE WEDGES SMALL",
"price": 7,
"slug": "crinckle-wedges-small-80052",
"sequence": 14
},
{
"itemId": 80053,
"name": "CRINCKLE WEDGES MEDIUM",
"description": "CRINCKLE WEDGES MEDIUM",
"price": 8,
"slug": "crinckle-wedges-medium-80053",
"sequence": 15
},
{
"itemId": 80054,
"name": "CRINCKLE WEDGES LARGE",
"description": "CRINCKLE WEDGES LARGE",
"price": 9,
"slug": "crinckle-wedges-large-80054",
"sequence": 16
},
]
现在我通过阵列必须循环,如果名下有两种小型,中型或大型我必须重新格式化数据,因此它应该是这个样子例如
{
"itemId": 80001,
"name": "FRENCH FRIES",
"description": "More delicious than ever, our signature piping hot, thick cut Salted French Fries are golden on the outside and fluffy on the inside.",
"itemModifiers":[
{
"itemId": 80001,
"name": "FRENCH FRIES SMALL",
"description": "More delicious than ever, our signature piping hot, thick cut Salted French Fries are golden on the outside and fluffy on the inside.",
"price": 6,
"slug": "french-fries-small-80001"
},
{
"itemId": 80002,
"name": "FRENCH FRIES MEDIUM",
"description": "More delicious than ever, our signature piping hot, thick cut Salted French Fries are golden on the outside and fluffy on the inside.",
"price": 7,
"slug": "french-fries-medium-80002"
},
{
"itemId": 80003,
"name": "FRENCH FRIES LARGE",
"description": "More delicious than ever, our signature piping hot, thick cut Salted French Fries are golden on the outside and fluffy on the inside.",
"price": 8,
"slug": "french-fries-large-80003"
}
],
"slug": "french-fries-80001",
"sequence": 8
}
客户的遗留系统设计不当,他们被要求适当的数据格式为更颗粒化格式的API。我试图弄清楚如何做到这一点。请注意,原始数组比我的例子有更多的项目,我应该循环每一个。我应该从头重建数据吗?或者有更好的方式来循环访问数组?
取而代之的是对数据进行额外处理,并且每次使用循环对大量数据进行重新格式化,如果根据您的方便重新构建原始数组,那听起来会更好。 –
您使用的是什么版本的Laravel? –
@RossWilson Laravel 5.1 –