我想根据(total_mark keys)值过滤数组。 我的意思是阵列过滤器最大值到最小值
$array = Array(
[0] => Array
(
"roll_n" => 1,
"total_mark" => 250
)
[1] => Array
(
"roll_n" => 2,
"total_mark" => 388
)
[2] => Array
(
"roll_n" => 3,
"total_mark" => 518
)
)
我的预期输出:
$array = Array(
[0] =Array
(
"roll_n" => 1,
"total_mark" => 518
)
[1] = Array
(
"roll_n" => 2,
"total_mark" => 388
)
)
提前致谢....
答案 0 :(得分:0)
您可以使用usort对多维数组类型进行排序:
usort($array, "custom_sort");
function custom_sort($a,$b) {
return $a['total_mark']<$b['total_mark'];
}
print_r ($array);
请参阅PHP参考:USORT
完整的PHP文件:
<?php
$array = Array(
Array
(
"roll_n" => 1,
"total_mark" => 250
), Array
(
"roll_n" => 2,
"total_mark" => 388
),Array
(
"roll_n" => 3,
"total_mark" => 518
)
);
echo '<pre>';
print_r($array);
echo '</pre><hr>';
echo '<pre>';
usort($array, "custom_sort");
function custom_sort($a,$b) {
return $a['total_mark']<$b['total_mark'];
}
$i = 0;
$array2 = array();
foreach ($array as $arr){
$arr['roll_n'] = $i;
$i++;
$array2[$i] = ['roll_n' => $i, 'total_mark' => $arr['total_mark']];
}
print_r ($array2); // Final Output
echo '</pre>';
?>