我有这个对象数组,需要删除键“ 0”:
"images": [
{
"0": "http://example.test/uploads/products/jqGfPyIUc_Wd.jpg"
},
{
"0": "http://example.test/uploads/products/bC1UIM5WwT8f.jpeg"
}
],
我的代码:
'images' => $this->images->map(function($item){
return (object)[$item->image_path];
}),
我需要删除键并将其保留为这样的数组:
"images": [
{
"http://example.test/uploads/products/jqGfPyIUc_Wd.jpg"
},
{
"http://example.test/uploads/products/bC1UIM5WwT8f.jpeg"
}
],
答案 0 :(得分:2)
对象属性应始终具有一个键。如果您不希望它具有键,则应将其存储为嵌套数组:
'images' => $this->images->map(function($item){
return array($item->image_path);
}),
// will create:
"images": [
[
"http://example.test/uploads/products/jqGfPyIUc_Wd.jpg"
],
[
"http://example.test/uploads/products/bC1UIM5WwT8f.jpeg"
]
],
或仅将其映射为字符串值并将其保留为标准数组:
'images' => $this->images->map(function($item){
return $item->image_path;
}),
// will create:
"images": [
"http://example.test/uploads/products/jqGfPyIUc_Wd.jpg",
"http://example.test/uploads/products/bC1UIM5WwT8f.jpeg"
],
答案 1 :(得分:0)
尝试这样:
'images' => $this->images->map(function($item){
return collect((object)[$item->image_path])->values();
}),
答案 2 :(得分:0)
该数组看起来像javascript。如果是这样,则可以将其映射如下。
viewbox
如果它是一个php数组,则它似乎来自console.log(images);
/*
[
{'0': 'http://example.test/uploads/products/jqGfPyIUc_Wd.jpg'},
{'0': 'http://example.test/uploads/products/bC1UIM5WwT8f.jpeg'}
]
*/
console.log(images.map(value => value[0]));
/*
[
'http://example.test/uploads/products/jqGfPyIUc_Wd.jpg',
'http://example.test/uploads/products/bC1UIM5WwT8f.jpeg'
]
*/
调用。您可以json_decode
。
map