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_Multidimensional Array - Fatal编程技术网

php多维数组排序问题

php多维数组排序问题,php,sorting,multidimensional-array,Php,Sorting,Multidimensional Array,我有一个复杂的多维数组。结构是这样的 Array ( [0] => Array ( [countries] => Array ( [0] => Array ( [country_code] => US

我有一个复杂的多维数组。结构是这样的

Array
(
    [0] => Array
        (
            [countries] => Array
                (
                    [0] => Array
                        (
                            [country_code] => US                            
                            [growth] => 3.57
                        )

                    [1] => Array
                        (
                            [country_code] => CA                            
                            [growth] => 4.77
                        )

                    [2] => Array
                        (
                            [country_code] => TT                            
                            [growth] => 0
                        )
                )
            [group_name] => North America
        )

    [1] => Array
        (
            [countries] => Array
                (
                    [0] => Array
                        (
                            [country_code] => BR                           
                            [growth] => 2.19
                        )

                    [1] => Array
                        (
                            [country_code] => PE                           
                            [growth] => 1.78
                        )

                    [2] => Array
                        (
                            [country_code] => UY                           
                            [growth] => 8.83
                        )

                    [3] => Array
                        (
                            [country_code] => MX                           
                            [growth] => 3.83
                        )
                )
            [group_name] => South America
        )
)
我想对它们进行排序(可以使用
array\u multisort
),以便根据
growth
(最高优先)对它们进行排序

这样,排序后的数组将

Array
(
    [0] => Array
        (
            [countries] => Array
                (
                    [0] => Array
                        (
                            [country_code] => CA                            
                            [growth] => 4.77
                        )
                    [1] => Array
                        (
                            [country_code] => US                            
                            [growth] => 3.57
                        )                 
                    [2] => Array
                        (
                            [country_code] => TT                            
                            [growth] => 0
                        )
                )
            [group_name] => North America
        )

    [1] => Array
        (
            [countries] => Array
                (
                   [0] => Array
                        (
                            [country_code] => UY                           
                            [growth] => 8.83
                        )

                   [1] => Array
                        (
                            [country_code] => MX                           
                            [growth] => 3.83
                        )
                    [2] => Array
                        (
                            [country_code] => BR                           
                            [growth] => 2.19
                        )

                    [3] => Array
                        (
                            [country_code] => PE                           
                            [growth] => 1.78
                        )
                )
            [group_name] => South America
        )
)

我是PHP新手,所以我不知道如何对这个复杂数组进行排序。我知道如何对siimple多维数组进行排序,如最坏情况下所示,您可以使用自己的排序功能。
它实际上是为这类事情而设计的


在您的情况下,您将通过
$arr[$i]['countries']
,并使用基于
$arr['growth']
的比较函数排序

我已经使用以下排序函数多年了:

/**
 * sorting array of associative arrays - multiple row sorting using a closure
 * see also: the-art-of-web.com/php/sortarray/
 * @param array $data input-array
 * @param string|array $fields array-keys
 * @license Public Domain
 * @return array
 */
function sortArray( $data, $field )
{
    $field = (array) $field;
    uasort( $data, function($a, $b) use($field) {
        $retval = 0;
        foreach( $field as $fieldname )
        {
            if( $retval == 0 ) $retval = strnatcmp( $a[$fieldname], $b[$fieldname] );
        }
        return $retval;
    } );
    return $data;
}


// example call, sort by 'growth' first and by 'country_code' afterwards
// this would be equal to a MySQL 'ORDER BY `growth` ASC, `country_code` ASC'
foreach( $countryArray as &$item )
{
    $item['countries'] = sortArray( $item['countries'], array( 'growth', 'country_code' ) );
}

我想对一个复杂的数组进行排序,但我不知道怎么做。我已经阅读了手册,但无法获得一个如此复杂数组的好例子。