PHP:按数组值的长度排序?

时间:2022-07-21 16:02:34

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.

我做了一个anagram机器,我有一个正匹配的数组。问题是它们的顺序都不一样,我想对数组进行排序,这样最长的数组值就会首先出现。

Anybody have any ideas on how to do this?

有人知道怎么做吗?

8 个解决方案

#1


128  

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

#2


51  

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.

但是,只有在源代码中的某个地方需要它时,才能这样做,以保持代码的干爽。

#3


20  

PHP7 is coming. In PHP7, you could use the Spaceship Operator.

PHP7即将来临。在PHP7中,你可以使用宇宙飞船操作员。

usort($array, function($a, $b) {
    return strlen($b) <=> strlen($a);
});

Hope this could help you in the future.

希望这对你将来有所帮助。

#4


10  

function sortByLength($a,$b){
  if($a == $b) return 0;
  return (strlen($a) > strlen($b) ? -1 : 1);
}
usort($array,'sortByLength');

#5


1  

Make an array of strlen of oyur array elements, and multisort it with your array.

创建一个oyur数组元素的strlen数组,并使用数组对其进行多重排序。

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!

这将绝对的工作。我相信!

#6


0  

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;
}

#7


-1  

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);

#8


-5  

It's simple.

这很简单。

function LSort(a,b){return a.length-b.length;}

var YourArray=[[1,2,3,4,5,6], ['a','b'], ['X','Y','Z'], ['I','Love','You'], ['good man']];

YourArray.sort(Lsort);

Result:

结果:

['good man'] Length=1
['a','b'] Length=3
['X','Y','Z'] Length=3
['I','Love','You'] Length=3
[1,2,3,4,5,6] Length=6

#1


128  

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

#2


51  

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.

但是,只有在源代码中的某个地方需要它时,才能这样做,以保持代码的干爽。

#3


20  

PHP7 is coming. In PHP7, you could use the Spaceship Operator.

PHP7即将来临。在PHP7中,你可以使用宇宙飞船操作员。

usort($array, function($a, $b) {
    return strlen($b) <=> strlen($a);
});

Hope this could help you in the future.

希望这对你将来有所帮助。

#4


10  

function sortByLength($a,$b){
  if($a == $b) return 0;
  return (strlen($a) > strlen($b) ? -1 : 1);
}
usort($array,'sortByLength');

#5


1  

Make an array of strlen of oyur array elements, and multisort it with your array.

创建一个oyur数组元素的strlen数组,并使用数组对其进行多重排序。

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!

这将绝对的工作。我相信!

#6


0  

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;
}

#7


-1  

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);

#8


-5  

It's simple.

这很简单。

function LSort(a,b){return a.length-b.length;}

var YourArray=[[1,2,3,4,5,6], ['a','b'], ['X','Y','Z'], ['I','Love','You'], ['good man']];

YourArray.sort(Lsort);

Result:

结果:

['good man'] Length=1
['a','b'] Length=3
['X','Y','Z'] Length=3
['I','Love','You'] Length=3
[1,2,3,4,5,6] Length=6