インデックスの関連付けとその他の文字列値を維持するには、次のコードを試してください。
function find_position($needle, $haystack) {
// put here the elements in order
//$defined_order = array('jan', 'fev', 'mar', 'abr', 'mai', 'jun', 'jul', 'ago', 'set', 'out', 'nov', 'dez');
$defined_order = array('jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec');
$needle_order = -1;
foreach($defined_order as $pos => $item) {
if($needle == $item) {
$needle_order = $pos;
break;
}
}
return($needle_order);
}
function order_array($unsorted_arr) {
$key_orders = array();
$not_available = array();
foreach($unsorted_arr as $key => $val) {
if($position = find_position($val, $unsorted_arr)) {
$key_orders[$key] = $position;
} else {
$not_available[$key] = $val;
}
}
asort($key_orders);
$ordered = array();
foreach($key_orders as $key => $posit) {
$ordered[$key] = $unsorted_arr[$key];
}
return($ordered);
// OR
// return(array('ordered'=>$ordered, 'not_available'=>$not_available));
}
$unsorted = array(0 => 'apr', 1 => 'aug', 2 => 'dec', 3 => 'feb', 4 => 'jan', 5 => 'jul', 6 => 'jun', 7 => 'mar', 8 => 'may', 9 => 'nov', 10 => 'oct', 11 => 'sep' );
$sorted = order_array($unsorted);