过滤器收集时删除键

我在将过滤器与Laravel 5.2一起使用时遇到了一个问题,过滤后,我得到了一些意想不到的键,如“0”,“1”,“2”......,我该如何删除它?

过滤前:

[
  {
    "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
  }
]

在过滤器之后,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
  }
}

如何删除结果中的键2,3和5,并且只得到一个数组,就像过滤前一样。任何帮助都是值得赞赏的。编辑:我的代码:

 $result = $result->filter(function ($item) {
                return $item->id > 5;
            })->all();

答案 1

尝试添加values()

$result = $result->filter(function ($item) {
                return $item->id > 5;
            })->values()->all();

答案 2

我在排序时遇到了同样的问题:这个例子是按分数和进球对比赛结果进行排序。排序在结果中添加键 attr。所以我在最后的->values()->all()中使用一个没有键的数组值。

例如:

$sorted = $resultados->sortByDesc('pts')->sortByDesc('gf')->values()->all();

在您的情况下:

$filteredValues = $filtered->values()->all();

我希望它能帮助你。


推荐