I've found a few answers to sorting by value, but not key.
我找到了一些按值排序的答案,但不是按键排序。
What I'd like to do is a reverse sort, so with:
我想做的是反向排序,因此
$nametocode['reallylongname']='12';
$nametocode['shortname']='10';
$nametocode['mediumname']='11';
I'd like them to be in this order
我希望它们按这个顺序排列
- reallylongname
- reallylongname
- mediumname
- mediumname
- shortname
- 短
mediumname shortname
mediumname短
Many thanks
非常感谢
10 个解决方案
#1
19
Another solution using array_multisort
:
使用对另一个解决方案:
$keys = array_map('strlen', array_keys($arr));
array_multisort($keys, SORT_DESC, $arr);
Here $keys
is an array of the lengths of the keys of $arr
. That array is sorted in descending order and then used to sort the values of $arr
using array_multisort
.
这里$keys是$arr中键的长度的数组。该数组按降序排序,然后使用array_multisort对$arr的值进行排序。
#2
8
You can use a user defined key sort function as a callback for uksort
:
您可以使用用户定义的密钥排序函数作为uksort的回调函数:
function cmp($a, $b)
{
if (strlen($a) == strlen($b))
return 0;
if (strlen($a) > strlen($b))
return 1;
return -1;
}
uksort($nametocode, "cmp");
foreach ($nametocode as $key => $value) {
echo "$key: $value\n";
}
Quick note - to reverse the sort simply switch "1" and "-1".
快速提示-要反向排序只需切换“1”和“-1”。
#3
8
Based on @thetaiko answer, with a simpler callback :
基于@thetaiko的答案,使用更简单的回调:
function sortByLengthReverse($a, $b){
return strlen($b) - strlen($a);
}
uksort($nametocode, "sortByLengthReverse");
Resources :
- php.net -
uksort()
- php.net作用是()
- php.net - Sorting arrays
- php.net排序数组
- php.net -
strlen()
- php.net strlen()
#5
2
Behold my powerful inline methodologies. Preserve global space for the generations to come!
看看我强大的内联方法。为子孙后代保护全球空间!
uksort($data, create_function('$a,$b', 'return strlen($a) < strlen($b);'));
#6
2
I have benchmarked some of sorting algorithms since performance is important for my project - here's what I've found (averaged result ran 1000x, sorted field had cca 300 elements with key size 3-50 chars):
我已经对一些排序算法进行了基准测试,因为性能对我的项目很重要——这是我发现的(平均结果是1000x,排序字段有cca 300个元素,密钥大小为3-50个字符):
- 2.01 sec ... uksort with anonymous create_function (by cojam)
- 2.01秒…带有匿名create_function的uksort(通过cojam)
- 0.28 sec ... array_multisort (by Gumbo)
- 0.28秒…对秋葵)
- 2.69 sec ... uksort with non-anonymous function (by Colin Herbert) - surprise for me,
- 2.69秒…uksort与非匿名函数(由科林·赫伯特)-给我惊喜,
- 0.15 sec ... simple foreach + arsort
- 0.15秒…简单的foreach + arsort
Sometime simple foreach still wins. Using dynamic PHP features has some performance penalty, obviously.
有时候简单的foreach仍然会赢。显然,使用动态PHP特性会降低性能。
#7
2
One limitation while sorting the keys on the basis of length is that: equal length keys are not re-ordered. Say we need to order the keys by length in descending
order.
根据长度对键进行排序时的一个限制是:不重新排序相同长度的键。假设我们需要按长度降序排列键。
$arr = array(
"foo 0" => "apple",
"foo 1" => "ball",
"foo 2 foo 0 foo 0" => "cat",
"foo 2 foo 0 foo 1 foo 0" => "dog",
"foo 2 foo 0 foo 1 foo 1" => "elephant",
"foo 2 foo 1 foo 0" => "fish",
"foo 2 foo 1 foo 1" => "giraffe"
);
debug($arr, "before sort");
$arrBad = $arr;
sortKeysDescBAD($arrBad);
debug($arrBad, "after BAD sort");
sortKeysDescGOOD($arr);
debug($arr, "after GOOD sort 2");
function sortKeysDescBAD(&$arrNew) {
$arrKeysLength = array_map('strlen', array_keys($arrNew));
array_multisort($arrKeysLength, SORT_DESC, $arrNew);
//return max($arrKeysLength);
}
function sortKeysDescGOOD(&$arrNew) {
uksort($arrNew, function($a, $b) {
$lenA = strlen($a); $lenB = strlen($b);
if($lenA == $lenB) {
// If equal length, sort again by descending
$arrOrig = array($a, $b);
$arrSort = $arrOrig;
rsort($arrSort);
if($arrOrig[0] !== $arrSort[0]) return 1;
} else {
// If not equal length, simple
return $lenB - $lenA;
}
});
}
function debug($arr, $title = "") {
if($title !== "") echo "<br/><strong>{$title}</strong><br/>";
echo "<pre>"; print_r($arr); echo "</pre><hr/>";
}
Output will be:
输出将:
before sort
Array
(
[foo 0] => apple
[foo 1] => ball
[foo 2 foo 0 foo 0] => cat
[foo 2 foo 0 foo 1 foo 0] => dog
[foo 2 foo 0 foo 1 foo 1] => elephant
[foo 2 foo 1 foo 0] => fish
[foo 2 foo 1 foo 1] => giraffe
)
after BAD sort
Array
(
[foo 2 foo 0 foo 1 foo 0] => dog
[foo 2 foo 0 foo 1 foo 1] => elephant
[foo 2 foo 0 foo 0] => cat
[foo 2 foo 1 foo 0] => fish
[foo 2 foo 1 foo 1] => giraffe
[foo 0] => apple
[foo 1] => ball
)
after GOOD sort
Array
(
[foo 2 foo 0 foo 1 foo 1] => elephant
[foo 2 foo 0 foo 1 foo 0] => dog
[foo 2 foo 1 foo 1] => giraffe
[foo 2 foo 1 foo 0] => fish
[foo 2 foo 0 foo 0] => cat
[foo 1] => ball
[foo 0] => apple
)
Notice the order of elephant
and dog
for example (or others) in two sorting methods. The second method looks better. There may be easier ways to solve this but hope this helps someone...
注意两种排序方法中大象和狗的顺序。第二种方法看起来更好。也许有更简单的方法来解决这个问题,但是希望这能帮助某人……
#8
0
The code below sorts the PHP hash array by string length of the key, then by the case-sensitive key itself, both in ascending order, using a lambda function:
下面的代码按照键的字符串长度对PHP哈希数组进行排序,然后通过区分大小写的密钥本身,使用lambda函数,这两种方法都是按升序排列的:
uksort($yourArray, function ($a, $b) {
return (strlen($a) == strlen($b) ? strcmp($a, $b) : strlen($a) - strlen($b));
});
This code does the same in reverse order:
此代码以相反的顺序执行:
uksort($yourArray, function ($b, $a) {
return (strlen($a) == strlen($b) ? strcmp($a, $b) : strlen($a) - strlen($b));
});
#9
0
In PHP7+ you can use uksort()
with spaceship operator and anonymous function like this:
在PHP7+中,您可以使用uksort()与太空船操作员和匿名函数如下:
uksort($array, function($a, $b) {
return strlen($b) <=> strlen($a);
});
#10
-1
A simple problem requires a simple solution ;-)
一个简单的问题需要一个简单的解决方案;
arsort($nametocode, SORT_NUMERIC);
$result = array_keys($nametocode);
#1
19
Another solution using array_multisort
:
使用对另一个解决方案:
$keys = array_map('strlen', array_keys($arr));
array_multisort($keys, SORT_DESC, $arr);
Here $keys
is an array of the lengths of the keys of $arr
. That array is sorted in descending order and then used to sort the values of $arr
using array_multisort
.
这里$keys是$arr中键的长度的数组。该数组按降序排序,然后使用array_multisort对$arr的值进行排序。
#2
8
You can use a user defined key sort function as a callback for uksort
:
您可以使用用户定义的密钥排序函数作为uksort的回调函数:
function cmp($a, $b)
{
if (strlen($a) == strlen($b))
return 0;
if (strlen($a) > strlen($b))
return 1;
return -1;
}
uksort($nametocode, "cmp");
foreach ($nametocode as $key => $value) {
echo "$key: $value\n";
}
Quick note - to reverse the sort simply switch "1" and "-1".
快速提示-要反向排序只需切换“1”和“-1”。
#3
8
Based on @thetaiko answer, with a simpler callback :
基于@thetaiko的答案,使用更简单的回调:
function sortByLengthReverse($a, $b){
return strlen($b) - strlen($a);
}
uksort($nametocode, "sortByLengthReverse");
Resources :
- php.net -
uksort()
- php.net作用是()
- php.net - Sorting arrays
- php.net排序数组
- php.net -
strlen()
- php.net strlen()
#4
#5
2
Behold my powerful inline methodologies. Preserve global space for the generations to come!
看看我强大的内联方法。为子孙后代保护全球空间!
uksort($data, create_function('$a,$b', 'return strlen($a) < strlen($b);'));
#6
2
I have benchmarked some of sorting algorithms since performance is important for my project - here's what I've found (averaged result ran 1000x, sorted field had cca 300 elements with key size 3-50 chars):
我已经对一些排序算法进行了基准测试,因为性能对我的项目很重要——这是我发现的(平均结果是1000x,排序字段有cca 300个元素,密钥大小为3-50个字符):
- 2.01 sec ... uksort with anonymous create_function (by cojam)
- 2.01秒…带有匿名create_function的uksort(通过cojam)
- 0.28 sec ... array_multisort (by Gumbo)
- 0.28秒…对秋葵)
- 2.69 sec ... uksort with non-anonymous function (by Colin Herbert) - surprise for me,
- 2.69秒…uksort与非匿名函数(由科林·赫伯特)-给我惊喜,
- 0.15 sec ... simple foreach + arsort
- 0.15秒…简单的foreach + arsort
Sometime simple foreach still wins. Using dynamic PHP features has some performance penalty, obviously.
有时候简单的foreach仍然会赢。显然,使用动态PHP特性会降低性能。
#7
2
One limitation while sorting the keys on the basis of length is that: equal length keys are not re-ordered. Say we need to order the keys by length in descending
order.
根据长度对键进行排序时的一个限制是:不重新排序相同长度的键。假设我们需要按长度降序排列键。
$arr = array(
"foo 0" => "apple",
"foo 1" => "ball",
"foo 2 foo 0 foo 0" => "cat",
"foo 2 foo 0 foo 1 foo 0" => "dog",
"foo 2 foo 0 foo 1 foo 1" => "elephant",
"foo 2 foo 1 foo 0" => "fish",
"foo 2 foo 1 foo 1" => "giraffe"
);
debug($arr, "before sort");
$arrBad = $arr;
sortKeysDescBAD($arrBad);
debug($arrBad, "after BAD sort");
sortKeysDescGOOD($arr);
debug($arr, "after GOOD sort 2");
function sortKeysDescBAD(&$arrNew) {
$arrKeysLength = array_map('strlen', array_keys($arrNew));
array_multisort($arrKeysLength, SORT_DESC, $arrNew);
//return max($arrKeysLength);
}
function sortKeysDescGOOD(&$arrNew) {
uksort($arrNew, function($a, $b) {
$lenA = strlen($a); $lenB = strlen($b);
if($lenA == $lenB) {
// If equal length, sort again by descending
$arrOrig = array($a, $b);
$arrSort = $arrOrig;
rsort($arrSort);
if($arrOrig[0] !== $arrSort[0]) return 1;
} else {
// If not equal length, simple
return $lenB - $lenA;
}
});
}
function debug($arr, $title = "") {
if($title !== "") echo "<br/><strong>{$title}</strong><br/>";
echo "<pre>"; print_r($arr); echo "</pre><hr/>";
}
Output will be:
输出将:
before sort
Array
(
[foo 0] => apple
[foo 1] => ball
[foo 2 foo 0 foo 0] => cat
[foo 2 foo 0 foo 1 foo 0] => dog
[foo 2 foo 0 foo 1 foo 1] => elephant
[foo 2 foo 1 foo 0] => fish
[foo 2 foo 1 foo 1] => giraffe
)
after BAD sort
Array
(
[foo 2 foo 0 foo 1 foo 0] => dog
[foo 2 foo 0 foo 1 foo 1] => elephant
[foo 2 foo 0 foo 0] => cat
[foo 2 foo 1 foo 0] => fish
[foo 2 foo 1 foo 1] => giraffe
[foo 0] => apple
[foo 1] => ball
)
after GOOD sort
Array
(
[foo 2 foo 0 foo 1 foo 1] => elephant
[foo 2 foo 0 foo 1 foo 0] => dog
[foo 2 foo 1 foo 1] => giraffe
[foo 2 foo 1 foo 0] => fish
[foo 2 foo 0 foo 0] => cat
[foo 1] => ball
[foo 0] => apple
)
Notice the order of elephant
and dog
for example (or others) in two sorting methods. The second method looks better. There may be easier ways to solve this but hope this helps someone...
注意两种排序方法中大象和狗的顺序。第二种方法看起来更好。也许有更简单的方法来解决这个问题,但是希望这能帮助某人……
#8
0
The code below sorts the PHP hash array by string length of the key, then by the case-sensitive key itself, both in ascending order, using a lambda function:
下面的代码按照键的字符串长度对PHP哈希数组进行排序,然后通过区分大小写的密钥本身,使用lambda函数,这两种方法都是按升序排列的:
uksort($yourArray, function ($a, $b) {
return (strlen($a) == strlen($b) ? strcmp($a, $b) : strlen($a) - strlen($b));
});
This code does the same in reverse order:
此代码以相反的顺序执行:
uksort($yourArray, function ($b, $a) {
return (strlen($a) == strlen($b) ? strcmp($a, $b) : strlen($a) - strlen($b));
});
#9
0
In PHP7+ you can use uksort()
with spaceship operator and anonymous function like this:
在PHP7+中,您可以使用uksort()与太空船操作员和匿名函数如下:
uksort($array, function($a, $b) {
return strlen($b) <=> strlen($a);
});
#10
-1
A simple problem requires a simple solution ;-)
一个简单的问题需要一个简单的解决方案;
arsort($nametocode, SORT_NUMERIC);
$result = array_keys($nametocode);