过滤器集合laravel时删除键

时间:2016-06-09 09:50:18

标签: php laravel laravel-5 laravel-5.1 laravel-5.2

我在使用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();

4 个答案:

答案 0 :(得分:48)

尝试添加values()

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

答案 1 :(得分:1)

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

享受!!

        $collection = collect([1, 2, 3, 4]);

        $filtered = $collection->filter(function ($item) {
            return $item < 2;
        });

        $filtered->all();
        return $filtered;

结果: [   1 ]

可是:

    $collection = collect([1, 2, 3, 4]);

    $filtered = $collection->filter(function ($item) {
        return $item > 2;
    });

    $filtered->all();
    return $filtered;

结果:{   “2”:3,   “3”:4 }

不知道怎么回事,为什么......

答案 2 :(得分:1)

排序时我遇到了同样的问题: 这个例子是按点和目标订购游戏结果。排序在结果中添加键attr。所以我在最后使用 - &gt; values() - &gt; all()来获取没有键的数组值。

例如:

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

在你的情况下:

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

我希望它可以帮到你。

答案 3 :(得分:0)

如果你想使用filter()帮助器,你不能这样做,因为这就是这个助手的工作原理。我的意思是这个方法没有参数或东西。您只能重建返回的集合。

或者,您可以使用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);
}

或者您可以使用带索引的集合。我的意思是通常你会使用集合来迭代它,这些指数不会打扰你。