Skip to content
Advertisement

PHP usort won’t sort class

This is a sample of the array of elemnts to sort:

JavaScript

So, let’s say I want to sort by brand_id. This is my usort callback function:

JavaScript

And when I do usort($items, '_compare'); var_dump($items); nothing happens. Any clues on how to troubleshoot this?

–UPDATE–

Ok, I’ve simplified the problem to this:

JavaScript

It always prints ‘Cant sort!

Advertisement

Answer

Finally, I discovered the source of this error. The problem was that this code was inside a class.

If that’s your case, then you should call usort this way:

JavaScript

Furthermore, if your Class is in a namespace, you should list the full namespace in usort.

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement