简体   繁体   English

数组键被“ uksort()”函数错误地排序

[英]Array keys being sorted incorrectly by 'uksort()' function

I have an array whose keys are in the format [A1] -> [A20], [B1] -> [B20], etc, and I'm trying to sort that array using first ksort() (to get the letters in the correct order) and then uksort() . 我有一个数组,其键的格式为[A1]-> [A20],[B1]-> [B20]等,我正在尝试使用第一个ksort()对该数组进行排序(以获取字母正确的顺序),然​​后uksort()

However, I can't figure out how the uksort() function should be written, and my array keys are coming back in a weird order. 但是,我不知道应该如何编写uksort()函数,而且我的数组键以奇怪的顺序返回。 Could someone please take a look and advise as to what I am doing wrong? 有人可以看看我的建议吗? Thanks. 谢谢。

function _sort_selection_keys($a, $b){

    $let_a = substr($a, 0, 1);
    $let_b = substr($b, 0, 1);
    $num_a = intval(substr($a, 1));
    $num_a = intval(substr($b, 1));

    /** Check that the first letter is the same. It should be, as the array has already been through 'ksort()', but it's worth checking any way */
    if($let_a !== $let_b) :
        return strcasecmp($a, $b);
    endif;

    if($num_a > $num_b) :
        return -1;
    elseif($num_a = $num_b) :
        return 0;
    elseif($num_a < $num_b) :
        return 1;
    endif;

}

You could just use the strnatcmp function which will fit your needs: 您可以只使用满足您需求的strnatcmp函数:

uksort($array, 'strnatcmp');

And another solution metioned by @M8R-1jmw5r @ M8R-1jmw5r提及的另一种解决方案

ksort($array, SORT_NATURAL);

Per the documentation for usort : 根据usort文档

The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second. 如果第一个参数被认为分别小于,等于或大于第二个参数,则比较函数必须返回小于,等于或大于零的整数。

That is, instead of returning true or false , you should return $num_a - $num_b (or do the comparison and return -1 , 1 , or 0 . 也就是说,不是返回, truefalse ,你应该返回$num_a - $num_b (或做比较,并返回-110

First, I think all you need to do is change your second "$num_a" line to "$num_b". 首先,我认为您要做的就是将第二行“ $ num_a”更改为“ $ num_b”。 Other than that, I would simply use strnatcasecmp() on the keys instead of all the dissecting you are attempting. 除此之外,我只会在键上使用strnatcasecmp(),而不是尝试剖析所有内容。

Without debugging your code there, you can use built-in functionality of the uksort to sort keys alphanumerically with 'strnatcmp'. 无需在此处调试代码,就可以使用uksort内置功能以'strnatcmp'字母数字方式对键进行排序。

Unsorted Array Code 未排序的数组代码

$array = array(
    'A1' => array(),
    'A2' => array(),
    'A3' => array(),
    'A4' => array(),
    'C1' => array(),
    'B2' => array(),
    'B1' => array(),
    'D1' => array(),
    'C2' => array(),
    'B3' => array(),
    'D3' => array(),
    'D2' => array(),
);

uksort($array, 'strnatcmp');

var_dump($array);

Result 结果

array (size=12)
  'A1' => 
    array (size=0)
      empty
  'A2' => 
    array (size=0)
      empty
  'A3' => 
    array (size=0)
      empty
  'A4' => 
    array (size=0)
      empty
  'B1' => 
    array (size=0)
      empty
  'B2' => 
    array (size=0)
      empty
  'B3' => 
    array (size=0)
      empty
  'C1' => 
    array (size=0)
      empty
  'C2' => 
    array (size=0)
      empty
  'D1' => 
    array (size=0)
      empty
  'D2' => 
    array (size=0)
      empty
  'D3' => 
    array (size=0)
      empty

You want to sort your array by the keys in natural order : 您想按键以自然顺序对数组进行排序:

ksort($array, SORT_NATURAL);

See ksort() . 参见ksort()

Natural sort order is: 自然排序顺序为:

regular:  A1, A10, A11, A2, A20, A21, ... A3, ...
natural:  A1, A2, A3, ..., A10, A11, ... A20, A21 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM