Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在筛选集合laravel时删除键_Php_Laravel_Laravel 5_Laravel 5.1_Laravel 5.2 - Fatal编程技术网

Php 在筛选集合laravel时删除键

Php 在筛选集合laravel时删除键,php,laravel,laravel-5,laravel-5.1,laravel-5.2,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": "

我在使用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
  }
}
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);
}
如何删除结果中的键2、3和5,并仅获得一个与筛选前类似的数组。任何帮助都将不胜感激。 编辑: 我的代码:


如果要使用
filter()
helper,就不能这样做,因为这正是这个helper的工作方式。我的意思是这个方法没有参数或其他东西。您只能重建返回的集合

或者,您可以使用
filter()
方法代码创建自己的助手,如
myFilter()
,并对其进行一些修改,例如:

{
  "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
  }
}
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);
}
或者您可以只使用带有索引的集合。我的意思是,通常您使用集合对其进行迭代,这些索引不会打扰您

$result = $result->filter(function ($item) {
                return $item->id < 5;
            })->all();
结果:{ "2": 3, "3": 4 }

不知道如何,为什么…

尝试添加
值()


我在排序时遇到了同样的问题: 示例是按分数和目标对游戏结果排序。排序将在结果中添加关键字属性。所以我在final->values()->all()中使用它来获得一个没有键的数组值

例如:

$sorted=$resultados->sortByDesc('pts')->sortByDesc('gf')->values()->all(); 就你而言:

$filteredValues = $filtered->values()->all(); $filteredValues=$filtered->values()->all();
我希望它能帮助您。

请给我您的代码,您尝试了什么?为什么不尝试
->拒绝(函数($row){return$row->id@SafoorSafdar:我试过了,但结果是一样的:(谢谢。也帮了我。我得到了一些稍微不同的东西。我没有使用$collection->values()->all(),而是使用$collecton->values().all()返回了一个数组,但值返回了一个无键集合如果不使用flant(),它还会删除键
$result = $result->filter(function ($item) {
                return $item->id > 5;
            })->values()->all();
$sorted = $resultados->sortByDesc('pts')->sortByDesc('gf')->values()->all(); $filteredValues = $filtered->values()->all();