无法理解为什么usort()将数组转换为1
这是我在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;
}
这是进行排序的代码
$someObj = new SomeClass();
$someObj->setSortKey('name');
$sorted_stuff = usort($stuff_to_sort, array($someObj, 'sortByKeyValue'));
其中$stuff_to_sort
为:
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
)
)
并且CCD_ 2得到值1而不是排序的数组。怎么了?
PHP 5.2.17
请参阅http://docs.php.net/function.usort:
bool-usort(array&$array,callback$cmp_function)
排序后的数组不是返回值。usort()会更改作为第一个参数传递的数组。