0

1つの要素(この場合、現在のユーザーに一致する要素)が上にバブルし、残りがその下に適切にソートされるように配列をソートすることも可能ですか?

私はこれでそれをやろうとしましたが(ええ、私のバージョンのPHPではクロージャを取得できません。これは残念です)、運がありませんでした。

    uasort($output, create_function('$a,$b',                                                        
        'if( $a["user_id"] == '.$this->user['id'].') { return -1; }'.                               
        'elseif( $b["user_id"] == '.$this->user['id'].') { return -1; }'.                           
        'elseif( $b["percent"] == $a["percent"]) { return strcasecmp($a["username"], $b["username"]); 
        'elseif($a["percent"] < $b["percent"]) { return 1; }'.                                      
        'elseif($a["percent"] > $b["percent"]) { return -1; }'.                                     
        'else { return 0; }'                                                                        
    ));

とにかく配列をソートする必要があるので、これがそれを行うための最良の方法だと思います-しかし、私は提案を受け入れています。

事前にThx。

4

1 に答える 1

1

おそらくこれを行うのが最善の方法です。

function sortWithUserFirst($output) {
    $user_ids = Set::extract('/user_id', $output); // list of user ids
    $user_id_key = array_search($this->user['id']);
    $user_record = $output[$user_id_key];
    unset($output[$user_id_key]); // clear the key => value pair
    uasort($output, your_sort_function);
    array_unshift($output, $user_record); // put the user record at the beginning of the array
}
于 2012-11-20T15:28:30.123 に答える