无法理解为什么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;
}
Run Code Online (Sandbox Code Playgroud)
这是进行排序的代码
$someObj = new SomeClass();
$someObj->setSortKey('name');
$sorted_stuff = usort($stuff_to_sort, array($someObj, 'sortByKeyValue'));
Run Code Online (Sandbox Code Playgroud)
在哪里$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
)
)
Run Code Online (Sandbox Code Playgroud)
和$sorted_stuff
获得排序后的数组的值1代替.怎么了 ?
PHP 5.2.17
见http://docs.php.net/function.usort:
bool usort(array&$ array,callback $ cmp_function)
排序数组不是返回值.usort()改变你传递的数组作为第一个参数.
归档时间: |
|
查看次数: |
147 次 |
最近记录: |