【问题标题】:Remove key when filter collections laravel过滤集合时删除键 laravel
【发布时间】:2016-06-09 09:50:17
【问题描述】:

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

【问题讨论】:

  • 请给我你的代码,你尝试了什么?
  • 为什么不试试->reject(function ($row) {return $row->id<5;});
  • @SafoorSafdar:我试过了,但它返回相同的结果:(

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


【解决方案1】:

尝试添加values()

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

【讨论】:

  • 谢谢。也帮助了我。
  • 我得到了一些不同的东西。而不是 $collection->values()->all(),我只使用了 $colleciton->values()。 all() 返回一个数组,但 values 返回一个无键集合
  • 我使用 flatten(),它也删除了键
  • 我认为不需要使用 all() 来处理文档,请在此处阅读详细信息 laravel.com/docs/8.x/collections#method-values
【解决方案2】:

我在排序时遇到了同样的问题: 该示例是按分数和目标对游戏结果进行排序。排序在结果中添加键 attr。所以我在最终的 ->values()->all() 中使用来获取没有键的数组值。

例如:

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

在你的情况下:

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

希望对你有帮助。

【讨论】:

    【解决方案3】:
    $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 }

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

    【讨论】:

      【解决方案4】:

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

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

      【讨论】:

        猜你喜欢
        • 2017-04-12
        • 2018-05-07
        • 2017-01-05
        • 2023-03-12
        • 2018-04-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-03
        相关资源
        最近更新 更多