这是待排序元素的示例数组:
$items =
array
0 =>
object(stdClass)[8]
public 'id' => string '110' (length=3)
public 'brand_id' => string '18' (length=2)
array
0 => string ' OT-708' (length=7)
public 'failed' => null
public 'diff' => null
1 =>
object(stdClass)[9]
public 'id' => string '161' (length=3)
public 'brand_id' => string '18' (length=2)
假设我想按brand_id
进行排序。
这是我的 usort 回调函数:
function _compare($itemA, $itemB){
if ($itemA->brand_id == $itemB->brand_id) {
return 0;
}
else{
return strcmp($itemA->brand_id, $itemB->brand_id); //just an example...
}
}
当我使用 usort($items, '_compare'); var_dump($items);
时,没有任何反应。有什么提示可以用来排除故障吗?
--更新--
好的,我已将问题简化为以下内容:
function cmp($itemA, $itemB){
return -1;
}
if (usort($items, "cmp"))
echo 'I just sorted!';
else echo 'Cant sort!';
它总是打印 '无法排序!'
usort
的返回值还是$items
?因为usort
只有在成功时返回布尔值,否则返回 false。 - GWW