How to remove index in collect array laravel withour looping

Viewed 822

I have collected an array in Laravel, but why there is an index in result? How to remove index without iterating in loop, since if data is big, looping will take many resources.

Here is the code

public function test(){
        $array = [ ["no"=>1],["no"=>2] , ["no"=>3], ["no"=>4] ] ;
        $collection = collect($array);

        $filtered = $collection->filter(function ($value, $key) {
            return $value['no'] > 2;
        });

        return $filtered->all();
    }

and this is the data result

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

and this what my expected result

{
    {
        "no": 3
    },
    {
        "no": 4
    }
}

expected result is without index, but how to make without for each or looping, i worry if using looping will take time letter

2 Answers

Simply use collection where() method

$array = [ ['no' => 1], ['no' => 2], ['no' => 3], ['no' => 4] ];
$collection = collect($array);

return $collection->where('no', '>', 2)->values();
Related