同一阵列的顶部我有一个数组:array_push或array_unshift多行PHP
[0] => Array
(
[Id] => 1
[Order] => 1
[ContentGroupId] => 10
[ContentGroupIsNew] => 0
)
[1] => Array
(
[Id] => 2
[Order] => 2
[ContentGroupId] => 11
[ContentGroupIsNew] => 0
)
[2] => Array
(
[Id] => 3
[Order] => 3
[ContentGroupId] => 12
[ContentGroupIsNew] => 1
)
[3] => Array
(
[Id] => 4
[Order] => 4
[ContentGroupId] => 13
[ContentGroupIsNew] => 1
)
[4] => Array
(
[Id] => 5
[Order] => 5
[ContentGroupId] => 14
[ContentGroupIsNew] => 0
)
默认顺序是[Order]
我要重新梳理这个阵列,使其订单由[ContentGroupIsNew] = 1
顶部(如果存在的话,在这个样本中2确实存在),但是保持其余元素的现有秩序。
如果我使用usort
函数,我可以得到[ContentGroupIsNew] = 1
,但其余的元素似乎得到随机排序,并且不保持原来的[Order]
值。
所以,最终的结果应该是这样的:
[0] => Array
(
[Id] => 3
[Order] => 3
[ContentGroupId] => 12
[ContentGroupIsNew] => 1
)
[1] => Array
(
[Id] => 4
[Order] => 4
[ContentGroupId] => 13
[ContentGroupIsNew] => 1
)
[2] => Array
(
[Id] => 1
[Order] => 1
[ContentGroupId] => 10
[ContentGroupIsNew] => 0
)
[3] => Array
(
[Id] => 2
[Order] => 2
[ContentGroupId] => 11
[ContentGroupIsNew] => 0
)
[4] => Array
(
[Id] => 5
[Order] => 5
[ContentGroupId] => 14
[ContentGroupIsNew] => 0
)
PHP代码:
function sort_array($b, $a) {
return $a['ContentGroupIsNew'] - $b['ContentGroupIsNew'];
}
usort($array, 'sort_array');
你的代码是什么样的?修改'usort()'函数中的条件应该很容易。 – jeroen
@jeroen添加了PHP以结束问题。 –