php 过滤集合 laravel 时删除键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37722490/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove key when filter collections laravel
提问by maphongba008
I ran into a problem when using filter with Laravel 5.2, after filtering, I got some unexpected key like "0", "1", "2" ..., how can I remove it?
我在 Laravel 5.2 中使用过滤器时遇到问题,过滤后,我得到了一些意想不到的键,如“0”、“1”、“2”……,我该如何删除它?
Before filter:
过滤前:
[
{
"id": 1,
"user_id": 11,
"location": "1",
"content": "1",
"interest_id": 1,
"longitude": 1,
"latitude": 1,
"place_id": "1",
"created_at": "2016-06-09 15:44:18",
"updated_at": "2016-06-02 14:28:42",
"deleted_at": null
},
{
"id": 2,
"user_id": 12,
"location": "Forest Lake QLD, Australia",
"content": "I'm newbie. Hello everybody",
"interest_id": 1,
"longitude": 152.9692508,
"latitude": -27.6236519,
"place_id": "ChIJB_NHl8hOkWsRMIne81qjAgU",
"created_at": "2016-06-09 14:28:42",
"updated_at": "2016-06-09 14:28:42",
"deleted_at": null
},
{
"id": 8,
"user_id": 11,
"location": "Hendra QLD, Australia",
"content": "What time is it?",
"interest_id": 1,
"longitude": 153.0635202,
"latitude": -27.4225981,
"place_id": "ChIJAXNg5PBYkWsRIIve81qjAgU",
"created_at": "2016-06-09 14:28:42",
"updated_at": "2016-06-09 14:28:42",
"deleted_at": null
},
{
"id": 9,
"user_id": 11,
"location": "Hendra QLD, Australia",
"content": "Nice Cream!!!!????????",
"interest_id": 2,
"longitude": 153.0635202,
"latitude": -27.4225981,
"place_id": "ChIJAXNg5PBYkWsRIIve81qjAgU",
"created_at": "2016-06-09 14:28:42",
"updated_at": "2016-06-09 14:28:42",
"deleted_at": null
},
{
"id": 4,
"user_id": 17,
"location": "Forest Lake QLD, úc",
"content": "Have a nice day!",
"interest_id": 1,
"longitude": 152.9692508,
"latitude": -27.6236519,
"place_id": "ChIJB_NHl8hOkWsRMIne81qjAgU",
"created_at": "2016-06-09 14:28:42",
"updated_at": "2016-06-09 14:28:42",
"deleted_at": null
},
{
"id": 7,
"user_id": 18,
"location": "Hendra QLD, Australia",
"content": "Where is Kiet Bui? ??????????",
"interest_id": 1,
"longitude": 153.0635202,
"latitude": -27.4225981,
"place_id": "ChIJAXNg5PBYkWsRIIve81qjAgU",
"created_at": "2016-06-09 14:28:42",
"updated_at": "2016-06-09 14:28:42",
"deleted_at": null
}
]
After filter, id > 5 for example:
过滤后,id > 5 例如:
{
"2": {
"id": 8,
"user_id": 11,
"location": "Hendra QLD, Australia",
"content": "What time is it?",
"interest_id": 1,
"longitude": 153.0635202,
"latitude": -27.4225981,
"place_id": "ChIJAXNg5PBYkWsRIIve81qjAgU",
"created_at": "2016-06-09 14:28:42",
"updated_at": "2016-06-09 14:28:42",
"deleted_at": null
},
"3": {
"id": 9,
"user_id": 11,
"location": "Hendra QLD, Australia",
"content": "Nice Cream!!!!????????",
"interest_id": 2,
"longitude": 153.0635202,
"latitude": -27.4225981,
"place_id": "ChIJAXNg5PBYkWsRIIve81qjAgU",
"created_at": "2016-06-09 14:28:42",
"updated_at": "2016-06-09 14:28:42",
"deleted_at": null
},
"5": {
"id": 7,
"user_id": 18,
"location": "Hendra QLD, Australia",
"content": "Where is Kiet Bui? ??????????",
"interest_id": 1,
"longitude": 153.0635202,
"latitude": -27.4225981,
"place_id": "ChIJAXNg5PBYkWsRIIve81qjAgU",
"created_at": "2016-06-09 14:28:42",
"updated_at": "2016-06-09 14:28:42",
"deleted_at": null
}
}
How can I remove the key 2, 3, and 5 in the result and only get an array like before filtering. Any help is appreciate. Edit: My code:
如何删除结果中的键 2、3 和 5,并只获得一个像过滤前一样的数组。任何帮助表示赞赏。编辑:我的代码:
$result = $result->filter(function ($item) {
return $item->id > 5;
})->all();
回答by mace
Try adding values()
尝试添加 values()
$result = $result->filter(function ($item) {
return $item->id > 5;
})->values()->all();
回答by Manuel Eduardo Romero
I had have the same problem when sorting: The example is ordering games result by points and goals. The sorting add key attr in the result. So I use in the final ->values()->all() to get an array values without keys.
我在排序时遇到了同样的问题:示例是按积分和目标对游戏结果进行排序。排序在结果中添加键 attr。所以我在最后使用 ->values()->all() 来获取没有键的数组值。
Eg:
例如:
$sorted = $resultados->sortByDesc('pts')->sortByDesc('gf')->values()->all();
In your case:
在你的情况下:
$filteredValues = $filtered->values()->all();
I hope it helps you.
我希望它能帮助你。
回答by rome ?
$result = $result->filter(function ($item) {
return $item->id < 5;
})->all();
Enjoy !!
享受 !!
$collection = collect([1, 2, 3, 4]);
$filtered = $collection->filter(function ($item) {
return $item < 2;
});
$filtered->all();
return $filtered;
result: [ 1 ]
结果:[1]
But:
但:
$collection = collect([1, 2, 3, 4]);
$filtered = $collection->filter(function ($item) {
return $item > 2;
});
$filtered->all();
return $filtered;
Result: { "2": 3, "3": 4 }
结果:{“2”:3,“3”:4}
don't know how, why...
不知道怎么,为什么...
回答by Alexey Mezenin
You can't do that if you want to use filter()
helper, because that's just how this helper works. I mean there are no parameters or something for this method. You can only rebuild returned collection.
如果你想使用filter()
helper,你不能这样做,因为这就是这个 helper 的工作方式。我的意思是这个方法没有参数或其他东西。您只能重建返回的集合。
Or, you can use filter()
method code to create your own helper, like myFilter()
and modify it a bit, for example:
或者,您可以使用filter()
方法代码来创建自己的助手,喜欢myFilter()
并稍微修改一下,例如:
public function myFilter(callable $callback)
{
$return = [];
foreach ($this->items as $key => $value) {
if ($callback($value, $key)) {
// $return[$key] = $value; // original line from filter() method
$return[] = $value; // Here you want to remove $key
}
}
return new static($return);
}
Or you could just use collection with indices. I mean usually you're using collection to iterate over it and these indices won't disturb you.
或者你可以只使用带有索引的集合。我的意思是通常你使用集合来迭代它,这些索引不会打扰你。