Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 按多个数组元素排序_Php_Sorting - Fatal编程技术网

Php 按多个数组元素排序

Php 按多个数组元素排序,php,sorting,Php,Sorting,我有一个如下所示的数组(示例): 我需要按国家排序,然后按memTypeID排序,同时保留数组键(在PHP中)。我认为我需要使用uksort,因为usort会重写数组键。我知道如何在一个数组元素上创建一个简单的比较函数,但我不知道如何处理两个数组元素 生成的数组应为: Array ( [1602] => Array ( [country] => Canada [memTypeID] => 9

我有一个如下所示的数组(示例):

我需要按国家排序,然后按memTypeID排序,同时保留数组键(在PHP中)。我认为我需要使用uksort,因为usort会重写数组键。我知道如何在一个数组元素上创建一个简单的比较函数,但我不知道如何处理两个数组元素

生成的数组应为:

Array
(
    [1602] => Array
        (
            [country] => Canada
            [memTypeID] => 9
        )
    [1600] => Array
        (
            [country] => Canada
            [memTypeID] => 10
        )

    [1601] => Array
        (
            [country] => United States
            [memTypeID] => 7
        )


)

使用usort可以正确排序,但不会保留数组键。如果我将uksort交换为usort,排序将丢失。修复后,再次检查,使用
uasort
Array
(
    [1602] => Array
        (
            [country] => Canada
            [memTypeID] => 9
        )
    [1600] => Array
        (
            [country] => Canada
            [memTypeID] => 10
        )

    [1601] => Array
        (
            [country] => United States
            [memTypeID] => 7
        )


)
function cmp($a, $b) {
     if(strcmp($a['country'],$b['country']) != 0) {
         return $a['country'] > $b['country'] ? 1 : -1;
     }
   return $a['memTypeID'] > $b['memTypeID'] ? 1 : -1;
}

uasort($a, "cmp");