简体   繁体   English

自然地按键排序多维数组

[英]Naturally sort a multi-dimensional array by key

I have a multidimensional array in php, and I want to naturally sort the array based on key value.我在 php 中有一个多维数组,我想自然地根据键值对数组进行排序。 The array in question:有问题的数组:

array(27) {
  ["user1"]=>
  array(1) {
        ["link"]=>
        string(24) "http://twitch.tv/example"
  }
  ["someotheruser"]=>
  array(1) {
        ["link"]=>
        string(24) "http://twitch.tv/example"
  }
  ["anotheruser"]=>
  array(1) {
        ["link"]=>
        string(24) "http://twitch.tv/example"
  }
  // etc...
}

I have attempted a few things so far, but I am having no luck.到目前为止,我已经尝试了一些事情,但我没有运气。 Using uksort with natsort doesn't work, and I don't want to have to go as far as writing a custom comparator for natural sorting order if I don't have to.uksort与 natsort 一起使用是行不通的,如果我不需要的话,我不想为自然排序顺序编写一个自定义比较器。 I also attempted sorting the keys individually, however that seemed to not work我还尝试单独对键进行排序,但这似乎不起作用

private function knatsort(&$array) {
    $keys = array_keys($array);
    natsort($keys);
    $new_sort = array();
    foreach ($keys as $keys_2) {
        $new_sort[$keys_2] = $array[$keys_2];
    }
    $array = $new_sort;
    return true;
}

Something simpler.更简单的东西。 Extract the array keys and sort those, sorting the original by that:提取数组键并对它们进行排序,按以下方式对原始键进行排序:

array_multisort(array_keys($array), SORT_NATURAL, $array);

With case insensitivity:不区分大小写:

array_multisort(array_keys($array), SORT_NATURAL | SORT_FLAG_CASE, $array);

Check out strnatcmp();查看strnatcmp(); This is a natural sort by key function using it:这是使用它的按键功能的自然排序:

function knatsort(&$arr){return uksort($arr,function($a, $b){return strnatcmp($a,$b);});}

Works in place with the speed and return value of uksort.与 uksort 的速度和返回值一起工作。 Use it like this:像这样使用它:

knatsort($array);

:) :)

Even simpler than using array_multisort : you can actually provide a sort flag to the built-in ksort function to make it sort an array by key in natural order:甚至比使用array_multisort更简单:您实际上可以为内置的ksort函数提供一个排序标志,以使其按自然顺序按键对数组进行排序:

$arr = array(
    "CFoo" => "xx1",
    "AFoo" => "xx2",
    "1Foo" => "xx3",
    "10AFoo" => "xx4"
);

ksort($arr, SORT_NATURAL);

Yields:产量:

Array
(
    [1Foo] => xx3
    [10AFoo] => xx4
    [AFoo] => xx2
    [CFoo] => xx1
)

If you want to apply it recursively to a multidimensional array, you can write a simple function for that:如果您想将它递归地应用于多维数组,您可以为此编写一个简单的函数:

function natksort_multi(&$arr = array()) {
    ksort($arr, SORT_NATURAL);

    foreach (array_keys($arr) as $key) {
        if (is_array($arr[$key])) {
            natksort_multi($arr[$key]);
        }
    }
}

Building off @AbraCadaver's answer from earlier, I managed to put together a one-line solution using a bitwise-or for combining natural sort with case-insensitivity.以@AbraCadaver 早先的回答为基础,我设法使用按位或将自然排序与不区分大小写结合起来的单行解决方案。

array_multisort(array_keys($this->streams), SORT_NATURAL | SORT_FLAG_CASE , $this->streams);

Where $this->streams is my multidimensional array. $this->streams是我的多维数组。 This seemed to work the best.这似乎效果最好。

只需使用选项SORT_NATURAL进行排序

sort($arr,SORT_NATURAL);

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

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