usort() PHP Function Issue with PHP 5.3

Most of you may be faced this issue, usort() behaves differently in PHP 5.1 & 5.3 versions.

E.g

<?php
class myClass {
  public static function mySortMethod($object1, $object2)
  {
     if ($object1->order == $object2->order) 
    {
         return 0;
    }
    return ($object1->order < $object2->order) ? -1 : 1;
  }
}

usort($arr, 'myClass: mySortMethod');

?>

This would be working fine till PHP 5.1, but in latest version means PHP 5.3 it doesn’t sort properly.

Solution:

There is a small change required while calling usort() and it will work fine.
use –

usort($arr, array(‘myClass’,’mySortMethod’));
This will solve issue of usort() sorting method with PHp 5.3 version.

Try it!!!

Published by Somnath Pawar

Software Engineer (LAMP)

One thought on “usort() PHP Function Issue with PHP 5.3

Leave a reply to itechonology Cancel reply