I have a function that I need help with. The purpose of the function is to sort an array of objects by field. This function was using the PHP function create_function which is now deprecated in PHP 7.2. I've been trying to rewrite the function, but the results return null. Any help in figuring out what's wrong is greatly appreciated.

function _list_sort(&$objects, $on, $order = 'ASC')
    usort($objects, function(&$a,&$b) use($on, $order) {
        return ($order === 'DESC') ? strcmp($a[$on],$b[$on]) : strcmp($a[$on],$b[$on]);

An array of object is not a list of objects. A list of object is an object by itself , so you can do a lot of things with it, you could create a /_SortableList class that would have the /_List as parent and sort the objects in this list by an index. I wrote more about ILM (Indexed List Model) at https://www.daniweb.com/programming/web-development/tutorials/500538/the-indexed-objects-list-model-ilm-for-oop-php-applications . If you understand this and you would like to create this child class here , I am in ( I will help you with that).

Sorting an array of object by a property value with a function is not only expensive in resources (time , memory , CPU) but also insufficient since it implies that the properties are public , breaking many OOP rules. PHP is not JavaScript nor Java so it needs its own approaches to solve common problems as sorting a list of objects based on the value of a property.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.