排序PHP数组没有ksort [英] Sorting PHP array without ksort
本文介绍了排序PHP数组没有ksort的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想手动排序,而不利用ksort的PHP数组。
I am trying to manually sort a PHP array without making use of ksort.
这是我的code如何看待当下:
This is how my code looks at the moment:
function my_ksort(&$arg){
foreach($arg as $key1 => $value1){
foreach($arg as $key2 => $value2){
if($key1 > $key2){
$aux = $value2;
$arg[$key2] = $value1;
$arg[$key1] = $aux;
}
}
}
}
这不排序,我无法弄清楚如何使它排序。
It doesn't sort, I can't figure out how to make it sort.
推荐答案
您可以试试这个:
function my_ksort(&$arg)
{
$keys=array_keys($arg);
sort($keys);
foreach($keys as $key)
{
$val=$arg[$key];
unset($arg[$key]);
$arg[$key]=$val;
}
}
我单独排序键,然后删除元素一个接一个,它们附加到年底,按升序排列。
I'm sorting the keys separately and then deleting the elements one-by-one and appending them to the end, in ascending order.
我在使用其他排序功能(排序()
),但如果你想消除从模拟所有可用的排序功能,排序()
更容易效仿。事实上, @ crypticous算法就是这样做的!
I'm using another sorting function (sort()
), but if you want to eliminate all available sorting functions from your emulation, sort()
is much easier to emulate. In fact, @crypticous's algorithm does just that!
这篇关于排序PHP数组没有ksort的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文