1. 程式人生 > >關聯陣列根據值進行排序

關聯陣列根據值進行排序

一維關聯型資料進行排序

private function sort_with_keyName($arr,$orderby='desc'){
        $new_array = array();
        $new_sort = array();
        foreach($arr as $key => $value){
            $new_array[] = $value;
        }
        if($orderby=='asc'){
            asort($new_array);
        }else{
            arsort($new_array);
        }
        foreach($new_array as $k => $v){
            foreach($arr as $key => $value){
                if($v==$value){
                    $new_sort[$key] = $value;
                    unset($arr[$key]);
                    break;
                }
            }
        }
        return $new_sort;
    }