I have 2 array when one of the array is empty and sortby then it gives empty array
$first = [];
$second = ['created_at'=>2];
$third= [$first , $second];
$res = collect($third)->sortBy('created_at')->first();
dd($res);
if both array have value then it proper run. issue comes when one of array is empty. i need other array which is not empty. Thanks
CodePudding user response:
replace :
$res = collect($third)->sortBy('created_at')->first();
by:
$res = collect($third)->sortBy('created_at')->filter(function($value) {return !empty($value);})->first();
this will remove any empty array from the result of the sort
CodePudding user response:
Try like this:
$first = collect([]);
$second = collect(['created_at'=>2]);
$third = $first->push($second)->sortBy('created_at')->first();