1
可以帮助将json结果修改为另一种结果格式。PHP Laravel将json结果重建为另一种json格式
我有此JSON
{
"_links": {...},
"total": 10,
"data": [
{
"_links": { ... },
"id": 1,
"name": "John Doe",
"email": "[email protected]",
"images": [
{
"imageid": 12,
"name": "Trees",
"url":"http://path/of/image.jpg"
},
{
"imageid": 13,
"name": "People",
"url":"http://path/of/image.jpg"
},
]
}
]
}
然后我的目标是要实现这种结果:所以基本上
{
"_links": { ... },
"total": 10,
"data": [
{
"_links": { ... }
"id": 1,
"name": "John Doe",
"email": "[email protected]",
"images": [
{
"12" : {
"name":"Trees",
"url":"http://path/of/image.jpg"
},
"13" : {
"name":"People",
"url":"http://path/of/image.jpg"
}
}
]
}
]
}
第二JSON我想要的图像标识是图像内的键名目的。我想收集laravel而不是循环。例如:
collect($results)->each(function($value, $key){
//code here...
});
请帮忙。谢谢
看看https://laravel.com/docs/5.4/collections#method-mapwithkeys。你也许可以用它在'data.images' – apokryfos
为什么,第一结构有意义和第二一个不 – RiggsFolly
@RiggsFolly是的,我知道,但我的客户希望这个结果是成功的,它很难解释这是好的,而不是那个。 :) – Cris