V
V
Vlad Avtomat2017-08-11 11:50:04
Arrays
Vlad Avtomat, 2017-08-11 11:50:04

How to sort values ​​in ascending order for an object by a specific field?

I get our object:
$partnerProductStatuses = PartnerProductsStatus::all();

id name count partner_id
7 sredn 30 53
6 mnogo 50 53 
8 maloo 10 53

After that, I need to sort by the count column.
So far I have found a similar solution, but it is only suitable for associative arrays, and I have an object ...
function cmp($a, $b)
    {
        return strcmp($a->count, $b->count);
    }
        usort($partnerProductStatuses, array($partnerProductStatuses,'cmp'));

In principle, it can be reduced to a simpler
$arr= [];
foreach ($partnerProductStatuses as $partnerProductStatus)
{
$arr['id'] = $partnerProductStatus->id;
$arr['count'] = $partnerProductStatus->count;
$arr['partner_id'] = $partnerProductStatus->partner_id;
}
But it still doesn't work for me... the debugger displays the following error:
"unsort() expects parameter 2 to be a valid callback, function 'cmp' not found or invalid function name"

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Pushkarev, 2017-08-11
@VladSavelev

And why not sort at the sampling level?
->orderBy('count', 'desc')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question