Remove key when filter collections laravel

37,680

Solution 1

Try adding values()

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

Solution 2

I had have the same problem when sorting: The example is ordering games result by points and goals. The sorting add key attr in the result. So I use in the final ->values()->all() to get an array values without keys.

Eg:

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

In your case:

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

I hope it helps you.

Solution 3

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

Enjoy !!

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

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

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

result: [ 1 ]

But:

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

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

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

Result: { "2": 3, "3": 4 }

don't know how, why...

Share:
37,680
maphongba008
Author by

maphongba008

Updated on August 11, 2020

Comments

  • maphongba008
    maphongba008 over 3 years

    I ran into a problem when using filter with Laravel 5.2, after filtering, I got some unexpected key like "0", "1", "2" ..., how can I remove it?

    Before filter:

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

    After filter, id > 5 for example:

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

    How can I remove the key 2, 3, and 5 in the result and only get an array like before filtering. Any help is appreciate. Edit: My code:

     $result = $result->filter(function ($item) {
                    return $item->id > 5;
                })->all();
    
  • Gabriel Augusto
    Gabriel Augusto over 6 years
    Thanks. Helped me too.
  • Tarek Adam
    Tarek Adam over 6 years
    I got something slightly different. Rather than $collection->values()->all() I just used $colleciton->values(). all() returned an array but values returned a keyless colleciton
  • Fred Lai
    Fred Lai over 3 years
    I use flatten(), it also removed the keys
  • Jazuly
    Jazuly over 2 years
    i think no need to use all() regarding documentation, read detail here laravel.com/docs/8.x/collections#method-values