移除Laravel中过滤集合时的键值

41

在使用 Laravel 5.2 中的 filter 函数时,我遇到了一个问题。在过滤后,我得到了一些意外的键,比如“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();

请给我你的代码,你尝试了什么? - rome 웃
为什么不尝试使用 ->reject(function ($row) {return $row->id<5;}); 呢? - Safoor Safdar
@SafoorSafdar: 我已经尝试过了,但它返回相同的结果 :( - maphongba008
4个回答

95

试着添加values()

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

谢谢。对我也有帮助。 - Gabriel Augusto
2
我得到了略微不同的东西。我只是使用了$collection->values(),而不是$collection->values()->all()。all()返回一个数组,但是values()返回一个无键集合。 - Tarek Adam
我使用flatten()函数,它也会删除键。 - Fred Lai
1
我认为不需要在文档中使用 all(),详细信息请阅读此处 https://laravel.com/docs/8.x/collections#method-values - Jazuly

5
我在排序时遇到了同样的问题: 例如,按积分和进球数对比赛结果进行排序。排序在结果中添加键属性。因此,我在最后使用->values() ->all()获取一个没有键的数组值。
例如: $sorted = $resultados->sortByDesc('pts')->sortByDesc('gf')->values()->all();
在你的情况下: $filteredValues = $filtered->values()->all();
希望这能帮到你。

1

如果你想使用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);
}

或者你可以直接使用带有索引的集合。我是说通常你使用集合来迭代它,这些索引不会干扰你。


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 }

不知道如何,为什么...


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接