array_push 或 array_unshift 多行到同一数组的顶部 PHP
array_push or array_unshift multiple rows to top of same array 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');
请试试这个
function myCmp($a, $b)
{
return strcmp($b["ContentGroupIsNew"], $a["ContentGroupIsNew"]);
}
uasort($array, "myCmp")
echo "<pre>"; print_r($array);
foreach ($data as $key => $row) {
$return_fare[$key] = $row['ContentGroupIsNew'];
$one_way_fare[$key] = $row['Id'];
}
array_multisort($return_fare, SORT_DESC, $one_way_fare, SORT_ASC, $data);
print_r($data);
在这里找到了这个解决方案:PHP sort array by two field values
我有一个数组:
[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');
请试试这个
function myCmp($a, $b)
{
return strcmp($b["ContentGroupIsNew"], $a["ContentGroupIsNew"]);
}
uasort($array, "myCmp")
echo "<pre>"; print_r($array);
foreach ($data as $key => $row) {
$return_fare[$key] = $row['ContentGroupIsNew'];
$one_way_fare[$key] = $row['Id'];
}
array_multisort($return_fare, SORT_DESC, $one_way_fare, SORT_ASC, $data);
print_r($data);
在这里找到了这个解决方案:PHP sort array by two field values