【发布时间】:2010-03-03 00:38:59
【问题描述】:
尝试根据 (1) 深度和 (2) 权重对这个对象数组进行排序,但不确定如何修改我正在使用的函数以包含这个其他级别...
我正在使用这个功能:
function cmp( $a, $b ) {
if( $a->weight == $b->weight ){
return 0 ;
}
return ($a->weight < $b->weight) ? -1 : 1;
}
然后这样做:
$menu = get_tree(4, $tid, -1, 2);
usort($menu, 'cmp');
这将根据权重对数组进行准确排序,但我需要添加另一个级别的排序。使数组先按深度排序,再按权重排序。
因此,如果原始数组如下所示:
Array
(
[0] => stdClass Object
(
[tid] => 24
[name] => Sample
[weight] => 3
[depth] => 0
)
[1] => stdClass Object
(
[tid] => 66
[name] => Sample Subcategory
[weight] => 0
[depth] => 1
)
[2] => stdClass Object
(
[tid] => 67
[name] => Another Example
[weight] => 1
[depth] => 0
)
[3] => stdClass Object
(
[tid] => 68
[name] => Subcategory for Another Example
[weight] => 1
[depth] => 1
)
[4] => stdClass Object
(
[tid] => 22
[name] => A third example master category
[weight] => 0
[depth] => 0
)
我可以先按深度排序,然后按重量排序,结果如下:
Array
(
[0] => stdClass Object
(
[tid] => 22
[name] => A third example master category
[weight] => 0
[depth] => 0
)
[1] => stdClass Object
(
[tid] => 67
[name] => Another Example
[weight] => 1
[depth] => 0
)
[2] => stdClass Object
(
[tid] => 24
[name] => Sample
[weight] => 3
[depth] => 0
)
[3] => stdClass Object
(
[tid] => 66
[name] => Sample Subcategory
[weight] => 0
[depth] => 1
)
[4] => stdClass Object
(
[tid] => 68
[name] => Subcategory for Another Example
[weight] => 1
[depth] => 1
)
【问题讨论】: