简体   繁体   English

PHP 按字母顺序然后按数字对数组键进行排序

[英]PHP sort array keys by alphabetically and then numerically

I am trying to sort an array (with both alphabetic and numeric keys) by keys where the alphabetic keys will come first alphabetically and then numeric keys numerically.我正在尝试按键对数组(带有字母和数字键)进行排序,其中字母键首先按字母顺序排列,然后按数字排列。

Tried ksort with all the available flags, however that didn't help.尝试使用所有可用标志进行ksort ,但这并没有帮助。 Tried several SO answers, but none of them served my purpose.尝试了几个 SO 答案,但没有一个符合我的目的。 Here is an example of the the array I have..这是我拥有的数组的一个例子..

$array = array(
    'Bat' => array(),
    'Dog' => array(),
    'Apple' => array(),
    'Cat' => array(),
    1 => array(),
    3 => array(),
    2 => array(),
    4 => array()
);  

I need to sort it like this:我需要这样排序:

$array = array(
    'Apple' => array(),
    'Bat' => array(),
    'Cat' => array(),
    'Dog' => array(),
    0 => array(),
    1 => array(),
    2 => array(),
    3 => array()
);  

What I understand from a SO answer that it might need a custom function to sort using the usort function.我从 SO answer 中了解到,它可能需要一个自定义函数来使用usort函数进行排序。 That's where I am lost.那就是我迷路的地方。 Any help or guide to the proper direction will be appreciated.任何有关正确方向的帮助或指南将不胜感激。

Thanks谢谢

If you use only english alphabet and digits, usual ksort function works fine如果您只使用英文字母和数字,通常的 ksort 函数可以正常工作

ksort($array);
print_r($array);

demo演示

Please don't mind the question.请不要介意这个问题。 I have managed achieve bu using a custom function.我已经使用自定义函数实现了 bu。 I am posting the answer here to help if someone faces this issue.如果有人遇到此问题,我将在此处发布答案以提供帮助。

function ev_sort_array( $array ) {
    $alp = array();
    $num = array();
    foreach ( $array as $key => $value ) {
        if ( is_numeric($key) ) {
            $num[$key] = $value;
        } else {
            $alp[$key] = $value;
        }
    }
    ksort( $alp );
    ksort( $num );

    return array_merge( $alp, $num );
}  

Thanks谢谢

You can use uksort this way:您可以这样使用 uksort:

$array = array(
'Bat' => array(),
'Dog' => array(),
'Apple' => array(),
'Cat' => array(),
1 => array(),
3 => array(),
2 => array(),
4 => array()
); 
uksort($array,function($a,$b){
    if(is_int($a)&&is_int($b)) return $a-$b;
    if(is_int($a)&&!is_int($b)) return 1;
    if(!is_int($a)&&is_int($b)) return -1;
    return strnatcasecmp($a,$b);
});
print_r($array);

the output is as expected:输出符合预期:

Array
(
    [Apple] => Array
        (
        )

    [Bat] => Array
        (
        )

    [Cat] => Array
        (
        )

    [Dog] => Array
        (
        )

    [1] => Array
        (
        )

    [2] => Array
        (
        )

    [3] => Array
        (
        )

    [4] => Array
        (
        )

)

As @splash58 states, ksort() is all that is required.正如@splash58 所说, ksort ()就是所需要的。

That said, if your project data is ordering numeric data before alphabetical and you need to reverse the order of those groups...也就是说,如果您的项目数据按字母顺序排列数字数据,并且您需要颠倒这些组的顺序......

uksort() is the most direct/appropriate function for this job. uksort()是这项工作最直接/最合适的函数。

*Choose your key evaluating function carefully: *谨慎选择您的关键评估函数:

  • is_numeric() has a broad definition of what qualifies as true -- see the manual. is_numeric()对什么是true有广泛的定义——请参阅手册。
  • is_int() (and its alias is_integer() ) is very strict in checking the data type. is_int() (及其别名is_integer() )在检查数据类型方面非常严格。 Importantly, a string key like "1" will evaluate to false .重要的是,像"1"这样的字符串键将评估为false
  • ctype_digit() , while requiring all characters to be numeric, does not work on non-string type values. ctype_digit()虽然要求所有字符都是数字,但不适用于非字符串类型值。 Importantly, a non-string key like 8 will evaluate to false .重要的是,像8这样的非字符串键将评估为false

If you are using php7, you can make good use the spaceship operator ( <=> ).如果您使用的是 php7,则可以很好地使用飞船操作符( <=> )。 If your php version is sub-7, you can use whatever old-skool comparison you like.如果您的 php 版本低于 7,您可以使用任何您喜欢的旧 skool 比较。

My solution allows the spaceship operator to order non-integers before integers, then sort those values ASC.我的解决方案允许飞船操作​​员在整数之前对非整数进行排序,然后按 ASC 对这些值进行排序。

Code: ( Demo )代码:( 演示

$array = [
    2 => [],
    'Bat' => [],
    'Dog' => [],
    12 => [],
    'être' => [],
    'Cat' => [],
    1 => [],
    3 => [],
    'Apple' => [],
    4 => []
];

uksort($array, function ($a, $b) {
    return [is_int($a), $a] <=> [is_int($b), $b];
});

var_export($array);

The is_int() calls will return true or false . is_int()调用将返回truefalse These are respectively compared as 1 and 0 .它们分别被比较为10 Since sorting ASC, this means that false evaluations will come before true evaluations.由于对 ASC 进行排序,这意味着false评估将在true评估之前出现。

Output: (exactly the same as ksort() )输出:(与ksort()完全相同)

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

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