2010-10-25 87 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