2010-10-25 97 views
0

以下陣列到達:自定義排序陣列鍵/值對

Array 
(
    [66-507cddcd16d9786abafccfa78b19acf8] => XL 
    [64-507cddcd16d9786abafccfa78b19acf8] => medium 
    [65-507cddcd16d9786abafccfa78b19acf8] => large 
    [63-507cddcd16d9786abafccfa78b19acf8] => small 
) 

如何訂購這樣的鍵/值關係保持上升中的大小順序排列的價值觀?數組值可以是部分或全部的以下

Small 
XXL 
Medium 
Large 
XL 
+0

見codaddict的評論。事實上,你可能會更好地編輯你的其他問題來代替它,所以我也可以相應地調整我的答案。 – BoltClock 2010-10-25 13:01:13

+0

[鏈接到上述其他問題的那些外行。](http://stackoverflow.com/questions/4014549/php-custom-ordering-array) – BoltClock 2010-10-25 13:10:21

回答

0

如果你想你的價值觀按字母順序排序,這就是asort是。

1

如果您整理的需要比asortksort如先前建議更復雜,然後寫一個函數堵塞到uasort

1

您需要使用uasort

function sizeSorter($a, $b) { 
    // customize as needed 
    $comp = array_flip(array('xxxs', 'xxs', 'xs', 's', 'small', 'm', 'medium', 'l', 'large', 'xl', 'xxl', 'xxxl')); 
    return $comp[strtolower($a)] - $comp[strtolower($b)]; 
} 

uasort($array, 'sizeSorter'); 

活生生的例子:在我回答你的其他問題

http://codepad.org/vxcN29sO