PHP: What causes usort() to turn array into 1?
Can't get why usort() turns array into 1
Here is code of my sorting callback method in SomeClass
protected $_sortKey = '';
public function setSortKey($keyname)
{
$this->_sortKey = $keyname;
}
public function sortByKeyValue($a, $b)
{
$key = $this->_sortKey;
if ($a->$key == $b->$key) {
return 0;
}
return ($a->$key < $b->$key) ? -1 : 1;
}
Here is code where sorting takes place
$someObj = new SomeClass();
$someObj->setSortKey('name');
$sorted_stuff = usort($stuff_to_sort, array($someObj, 'sortByKeyValue'));
开发者_Python百科
where $stuff_to_sort
is:
Array
(
[0] => stdClass Object
(
[id] => 57
[status] => ACTIVE
[updated] => 2010-09-17T12:16:25Z
[name] => Windows Server 2008 SP2 x64 - MSSQL2K8R2
)
[1] => stdClass Object
(
[id] => 62
[status] => ACTIVE
[updated] => 2010-10-19T17:16:55Z
[name] => Red Hat Enterprise Linux 5.5
)
)
and $sorted_stuff
gets value 1 instead of sorted array. What is wrong ?
PHP 5.2.17
see http://docs.php.net/function.usort:
bool usort ( array &$array , callback $cmp_function )
The sorted array is not the return value. usort() alters the array you're passing as the first argument.
精彩评论