Array ( [0] => Array ( [1] => Array ( [EmpId] => STU100 [EmpName] => XXXXXX [percentage] => [max_point] => [opt_point] => )
[2] => Array ( [EmpId] => STU100 [EmpName] => XXXXXX [percentage] => [max_point] => [opt_point] => )
[3] => Array ( [EmpId] => STU100 [EmpName] => XXXXXX [percentage] => [max_point] => [opt_point] => )
[4] => Array ( [EmpId] => STU100 [EmpName] => XXXXXX [percentage] => [max_point] => [opt_point] => )
[5] => Array ( [EmpId] => STU100 [EmpName] => XXXXXX [percentage] => [max_point] => [opt_point] => )
[6] => Array ( [EmpId] => STU100 [EmpName] => XXXXXX [percentage] => [max_point] => [opt_point] => )
[7] => Array ( [EmpId] => STU100 [EmpName] => XXXXXX [percentage] => [max_point] => [opt_point] => )
[8] => Array ( [EmpId] => STU100 [EmpName] => XXXXXX [percentage] => [max_point] => [opt_point] => )
[9] => Array ( [EmpId] => STU100 [EmpName] => XXXXXX [percentage] => [max_point] => [opt_point] => )
[10] => Array ( [EmpId] => STU100 [EmpName] => XXXXXX [percentage] => [max_point] => [opt_point] => )
[11] => Array ( [EmpId] => STU100 [EmpName] => XXXXXX [percentage] => [max_point] => [opt_point] => )
[12] => Array ( [EmpId] => STU100 [EmpName] => XXXXXX [percentage] => [max_point] => [opt_point] => ) )
[1] => Array ( [1] => Array ( [EmpId] => STU101 [EmpName] => YYYYY [percentage] => [max_point] => [opt_point] => )
[2] => Array ( [EmpId] => STU101 [EmpName] => YYYYY [percentage] => [max_point] => [opt_point] => )
[3] => Array ( [EmpId] => STU101 [EmpName] => YYYYY [percentage] => [max_point] => [opt_point] => )
[4] => Array ( [EmpId] => STU101 [EmpName] => YYYYY [percentage] => [max_point] => [opt_point] => )
[5] => Array ( [EmpId] => STU101 [EmpName] => YYYYY [percentage] => [max_point] => [opt_point] => )
[6] => Array ( [EmpId] => STU101 [EmpName] => YYYYY [percentage] => [max_point] => [opt_point] => )
[7] => Array ( [EmpId] => STU101 [EmpName] => YYYYY [percentage] =>90 [max_point] => [opt_point] => )
[8] => Array ( [EmpId] => STU101 [EmpName] => YYYYY [percentage] => [max_point] => [opt_point] => )
[9] => Array ( [EmpId] => STU101 [EmpName] => YYYYY [percentage] => [max_point] => [opt_point] => )
[10] => Array ( [EmpId] => STU101 [EmpName] => YYYYY [percentage] => [max_point] => [opt_point] => )
[11] => Array ( [EmpId] => STU101 [EmpName] => YYYYY [percentage] => [max_point] => [opt_point] => )
[12] => Array ( [EmpId] => STU101 [EmpName] => YYYYY [percentage] => [max_point] => [opt_point] => )
[2] => Array ( [1] => Array ( [EmpId] => STU102 [EmpName] => YYYYY [percentage] => [max_point] => [opt_point] => )
[2] => Array ( [EmpId] => STU102 [EmpName] => YYYYY [percentage] => [max_point] => [opt_point] => )
[3] => Array ( [EmpId] => STU102 [EmpName] => YYYYY [percentage] => [max_point] => [opt_point] => )
[4] => Array ( [EmpId] => STU102 [EmpName] => YYYYY [percentage] => [max_point] => [opt_point] => )
[5] => Array ( [EmpId] => STU102 [EmpName] => YYYYY [percentage] => [max_point] => [opt_point] => )
[6] => Array ( [EmpId] => STU102 [EmpName] => YYYYY [percentage] => [max_point] => [opt_point] => )
[7] => Array ( [EmpId] => STU102 [EmpName] => YYYYY [percentage] =>100 [max_point] => [opt_point] => )
[8] => Array ( [EmpId] => STU102 [EmpName] => YYYYY [percentage] => [max_point] => [opt_point] => )
[9] => Array ( [EmpId] => STU102 [EmpName] => YYYYY [percentage] => [max_point] => [opt_point] => )
[10] => Array ( [EmpId] => STU102 [EmpName] => YYYYY [percentage] => [max_point] => [opt_point] => )
[11] => Array ( [EmpId] => STU102 [EmpName] => YYYYY [percentage] => [max_point] => [opt_point] => )
[12] => Array ( [EmpId] => STU102 [EmpName] => YYYYY [percentage] => [max_point] => [opt_point] => ) ).....
我需要根据[百分比]排序的数组,例如:STU102是第1,STU100是第2,STU100是第3,基于这些方式我需要得到输出,任何人都可以帮助我。 我试过了, 当我使用这种方法时,
function cmp($a, $b) {
return $b[7]["percentage"] - $a[7]["percentage"];
}
usort($per_details1, "cmp");
它被分类了。但我直接给了$ b [7] ["百分比"] - $ a [7] ["百分比"]第7个索引。
答案 0 :(得分:1)
function sort($a, $b) {
if ($a['percentage'] == $b['percentage']) return 0;
return ($a['percentage'] > $b['percentage']) ? 1 : -1;
}
function sort(&$value) {
usort($value, 'sort');
}
array_walk($array, 'sort');
答案 1 :(得分:1)
循环遍历数组并使用NonUniqueResultException
按子数组中的usort()
字段对它们进行排序。在percentage
循环中使用引用变量,以便它在阵列上运行。
foreach
答案 2 :(得分:1)
混合使用array_walk
和usort
;你可以完成工作。下面的代码说明了如何。
简介:完成工作
<?php
// FIRST; SORT THE MAIN ARRAY IN REVERSE ORDER...
rsort($mdArray);
// IF YOU WANT TO SORT BY ANY OTHER FIELD, SAY "EmpId"
// YOU CAN SIMPLY CHANGE THE DEFAULT VALUE OF $field to "EmpId"
function sortByField($prev, $next, $field="percentage") {
return $next[$field] - $prev[$field];
}
array_walk($mdArray, function(&$data){
usort($data, "sortByField");
});
详细信息:使用示例数组:
<?php
$mdArray = array(
array (
array( "EmpId" => "STU100", "EmpName" => "XXXXXX", "percentage" => 15, "max_point" => null, "opt_point" => null, ),
array( "EmpId" => "STU100", "EmpName" => "XXXXXX", "percentage" => 78, "max_point" => null, "opt_point" => null, ),
array( "EmpId" => "STU100", "EmpName" => "XXXXXX", "percentage" => 20 ,"max_point" => null, "opt_point" => null, ),
array( "EmpId" => "STU100", "EmpName" => "XXXXXX", "percentage" => 10, "max_point" => null, "opt_point" => null, ),
array( "EmpId" => "STU100", "EmpName" => "XXXXXX", "percentage" => 77, "max_point" => null, "opt_point" => null, ),
array( "EmpId" => "STU100", "EmpName" => "XXXXXX", "percentage" => 34, "max_point" => null, "opt_point" => null, ),
array( "EmpId" => "STU100", "EmpName" => "XXXXXX", "percentage" => 22, "max_point" => null, "opt_point" => null, ),
array( "EmpId" => "STU100", "EmpName" => "XXXXXX", "percentage" => 78, "max_point" => null, "opt_point" => null, ),
array( "EmpId" => "STU100", "EmpName" => "XXXXXX", "percentage" => 49, "max_point" => null, "opt_point" => null, ),
array( "EmpId" => "STU100", "EmpName" => "XXXXXX", "percentage" => 55, "max_point" => null, "opt_point" => null, ),
array( "EmpId" => "STU100", "EmpName" => "XXXXXX", "percentage" => 36, "max_point" => null, "opt_point" => null, ),
array( "EmpId" => "STU100", "EmpName" => "XXXXXX", "percentage" => 18, "max_point" => null, "opt_point" => null,)
),
array (
array( "EmpId" => "STU101", "EmpName" => "YYYYY", "percentage" => 70, "max_point" => null, "opt_point" => null, ),
array( "EmpId" => "STU101", "EmpName" => "YYYYY", "percentage" => 54, "max_point" => null, "opt_point" => null, ),
array( "EmpId" => "STU101", "EmpName" => "YYYYY", "percentage" => 23, "max_point" => null, "opt_point" => null, ),
array( "EmpId" => "STU101", "EmpName" => "YYYYY", "percentage" => 46, "max_point" => null, "opt_point" => null, ),
array( "EmpId" => "STU101", "EmpName" => "YYYYY", "percentage" => 10, "max_point" => null, "opt_point" => null, ),
array( "EmpId" => "STU101", "EmpName" => "YYYYY", "percentage" => 21, "max_point" => null, "opt_point" => null, ),
array( "EmpId" => "STU101", "EmpName" => "YYYYY", "percentage" => 80, "max_point" => null, "opt_point" => null, ),
array( "EmpId" => "STU101", "EmpName" => "YYYYY", "percentage" => 34, "max_point" => null, "opt_point" => null, ),
array( "EmpId" => "STU101", "EmpName" => "YYYYY", "percentage" => 50, "max_point" => null, "opt_point" => null, ),
array( "EmpId" => "STU101", "EmpName" => "YYYYY", "percentage" => 27, "max_point" => null, "opt_point" => null, ),
array( "EmpId" => "STU101", "EmpName" => "YYYYY", "percentage" => 39, "max_point" => null, "opt_point" => null, ),
array( "EmpId" => "STU101", "EmpName" => "YYYYY", "percentage" => 12, "max_point" => null, "opt_point" => null, ),
),
);
// FIRST; SORT THE MAIN ARRAY IN REVERSE ORDER...
rsort($mdArray);
// IF YOU WANT TO SORT BY ANY OTHER FIELD, SAY "EmpId"
// YOU CAN SIMPLY CHANGE THE DEFAULT VALUE OF $field to "EmpId"
function sortByField($prev, $next, $field="percentage") {
return $next[$field] - $prev[$field];
}
array_walk($mdArray, function(&$data){
usort($data, "sortByField");
});
var_dump($mdArray);
上面的var_dump
会产生类似下面的结果。现在请注意,数组按降序中的百分比排序。如果您想要逆转流程(即按升序排序),只需在函数内部({1}}内部$next
和$prev
交换。
sortByField
测试here。
答案 3 :(得分:0)
发布时,您确实应该格式化代码。
您可以使用PHP的用户来完成您之后的工作。请参阅accepted answer here。
function cmp($a, $b) {
return $a["mid"] - $b["mid"];
}
usort($arr, "cmp");
此外,虽然我们很乐意在此处提供帮助,但您也应该对自己的问题进行一些自己的研究。