我正试图弄清楚如何对一个数组进行排序,这个数组有子数组,每个子数组都有一个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之前,我已经按值进行了排序,但我不知道如何对这样的多维数组进行排序和维护索引关联。任何帮助都会很棒!
对我来说是一个很好的选择:
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;
}
}