Chartjs-How To get keys(index) of collection in laravel

4๐Ÿ‘

โœ…

You can use filter collection helper.

$result = $yourCollection->filter(function ($value, $key) {
    return in_array($key, ['01', '02', '03', '04', '05']);
});

Or externally if you want to pass that array,

$monthArr =   ['01', '02', '03', '04', '05'];
$result = $yourCollection->filter(function ($value, $key) use($monthArr) 
{
    return in_array($key, $monthArr);
});

EDIT

If you want to get only keys of collection then,

$keys = $yourCollection->keys();
dd($keys);

Here is link of official documentation.

1๐Ÿ‘

if you want to get only the keys of the collections you can use keys method

Leave a comment