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