我试图弄清楚如何对数组进行排序,该数组具有子阵列,每个子阵列都有一个X& Y坐标。我需要先将最大的大小排序到最小的,例如:
$sizes = array(
'a' => array(
'x' => 10,
'y' => 140,
),
'b' => array(
'x' => 20,
'y' => 24,
),
'c' => array(
'x' => 20,
'y' => 40,
),
'd' => array(
'x' => 50,
'y' => 50,
),
'e' => array(
'x' => 10,
'y' => 9,
),
);
应该如此自然地采取行动:
$sizes = array(
'e' => array(
'x' => 10,
'y' => 9,
),
'b' => array(
'x' => 20,
'y' => 24,
),
'c' => array(
'x' => 20,
'y' => 40,
),
'd' => array(
'x' => 50,
'y' => 50,
),
'a' => array(
'x' => 10,
'y' => 140,
),
);
我在使用asort之前按值排序,但是我不确定如何在多维数组上对这样的索引关联进行排序和维护。任何帮助都会很棒!
答案 0 :(得分:0)
对我来说很棒:
function sort_multi_array_2_col($arrInbound
, $strColumn1
, $strColumn1Sort = "ASC"
, $strColumn2
, $strColumn2Sort = "ASC") {
reset($arrInbound);
foreach ($arrInbound as $key => $row) {
$columnToSort1[$key] = $row[$strColumn1];
$columnToSort2[$key] = $row[$strColumn2];
}
array_multisort($columnToSort1
, get_sort_flag($strColumn1Sort)
, $columnToSort2
, get_sort_flag($strColumn2Sort)
, $arrInbound);
return $arrInbound;
}
function get_sort_flag($strSort) {
if($strSort == "ASC"){
return SORT_ASC;
} else {
return SORT_DESC;
}
}