1
排序多維數組使用鍵排序多維數組
$list = array_sort($list, 'name', SORT_ASC);
function array_sort($array, $on, $order = SORT_ASC)
{
$new_array = array();
$sortable_array = array();
if (count($array) > 0) {
foreach ($array as $k => $v) {
if (is_array($v)) {
foreach ($v as $k2 => $v2) {
if ($k2 == $on) {
$sortable_array[$k] = $v2;
}
}
} else {
$sortable_array[$k] = $v;
}
}
switch ($order) {
case SORT_ASC:
asort($sortable_array);
break;
case SORT_DESC:
arsort($sortable_array);
break;
}
foreach ($sortable_array as $k => $v) {
$new_array[$k] = $array[$k];
}
}
return $new_array;
}
//Prepare Array List
$list = array(
array('type' => 'suite', 'name' => 'A-Name'),
array('type' => 'suite', 'name' => 'C-Name'),
array('type' => 'suite', 'name' => 'B-Name'),
);
$list = array_sort($list, 'name', SORT_ASC);
你遇到什麼錯誤? – webpic
我想使用多個鍵排序數組 –
@GohilVishal也請分享您的預期輸出。 –