PHP:按其值的长度对数组进行排序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/838227/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP: Sort an array by the length of its values?
提问by Sam152
I made an anagram machine and I have an array of positive matches. The trouble is they are all in a different order, I want to be able to sort the array so the longest array values appear first.
我制作了一个字谜机器,我有一系列正匹配。问题是它们的顺序不同,我希望能够对数组进行排序,以便最长的数组值首先出现。
Anybody have any ideas on how to do this?
有人对如何做到这一点有任何想法吗?
回答by Unknown
Use http://us2.php.net/manual/en/function.usort.php
使用http://us2.php.net/manual/en/function.usort.php
with this custom function
使用此自定义功能
function sort($a,$b){
return strlen($b)-strlen($a);
}
usort($array,'sort');
Use uasort if you want to keep the old indexes, use usort if you don't care.
如果您想保留旧索引,请使用 uasort,如果您不关心,请使用 usort。
Also, I believe that my version is better because usort is an unstable sort.
另外,我相信我的版本更好,因为 usort 是一种不稳定的排序。
$array = array("bbbbb", "dog", "cat", "aaa", "aaaa");
// mine
[0] => bbbbb
[1] => aaaa
[2] => aaa
[3] => cat
[4] => dog
// others
[0] => bbbbb
[1] => aaaa
[2] => dog
[3] => aaa
[4] => cat
回答by xlttj
If you'd like to do it the PHP 5.3 way, you might want to create something like this:
如果你想用 PHP 5.3 的方式来做,你可能想要创建这样的东西:
usort($array, function($a, $b) {
return strlen($b) - strlen($a);
});
This way you won't pollute your global namespace.
这样你就不会污染你的全局命名空间。
But do this only if you need it at a single place in your source code to keep things DRY.
但是,只有在源代码中的一个地方需要它以保持干燥时才这样做。
回答by ZhenhangTung
PHP7 is coming. In PHP7, you could use the Spaceship Operator.
PHP7 来了。在 PHP7 中,您可以使用Spaceship Operator。
usort($array, function($a, $b) {
return strlen($b) <=> strlen($a);
});
Hope this could help you in the future.
希望这可以在将来对您有所帮助。
回答by zalew
function sortByLength($a,$b){
if($a == $b) return 0;
return (strlen($a) > strlen($b) ? -1 : 1);
}
usort($array,'sortByLength');
回答by Moinkhan Pathan
Make an array of strlenof oyur array elements, and multisortit with your array.
制作一组strlenoyur 数组元素,并将multisort其与您的数组一起使用。
foreach($Yourarray as $c=>$key) {
$key['maxlen'] = strlen($key);
$sort_numcie[] = $key['maxlen'];
}
array_multisort($sort_numcie, $Yourarray);
This will definately work. I am sure!
这肯定会奏效。我相信!
回答by RafaSashi
In addition to the accepted answer, to sort an array by length with ascending OR descending order:
除了接受的答案之外,要按长度按升序或降序对数组进行排序:
function strlen_compare($a,$b){
if(function_exists('mb_strlen')){
return mb_strlen($b) - mb_strlen($a);
}
else{
return strlen($b) - strlen($a);
}
}
function strlen_array_sort($array,$order='dsc'){
usort($array,'strlen_compare');
if($order=='asc'){
$array=array_reverse($array);
}
return $array;
}
回答by hgp
array_multisort(array_map('count', $arr), SORT_DESC, $arr);
回答by will
Here's a way using the spaceship operator (needs PHP 7.0):
下面是一种使用飞船操作符的方法(需要 PHP 7.0):
$arr = ['apple','pear','oranges','banana'];
usort($arr, function ($a, $b) { return (strlen($a) <=> strlen($b)); });
print_r($arr);
回答by Swadhin Saha
Descending Order:
降序排列:
$array = ['aa', 'bb', 'c', 'ccc', 'a', 'ertre'];
usort($array, function($a, $b){
return strlen($a) < strlen($b);
});
var_export($array);
// Output
array (
0 => 'ertre',
1 => 'ccc',
2 => 'aa',
3 => 'bb',
4 => 'c',
5 => 'a',
)
Ascending Order:
升序:
$array = ['aa', 'bb', 'c', 'ccc', 'a', 'ertre'];
usort($array, function($a, $b){
return strlen($a) > strlen($b);
});
// Output
array (
0 => 'c',
1 => 'a',
2 => 'aa',
3 => 'bb',
4 => 'ccc',
5 => 'ertre',
)
回答by joeb
Here's a way I've done it in the past.
这是我过去做过的一种方式。
// Here's the sorting...
$array = array_combine($words, array_map('strlen', $words));
arsort($array);

