PHP多维数组排序问题

3

我有一个复杂的多维数组。结构如下:

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_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,所以不知道如何对这个复杂的数组进行排序。我了解如何对简单的多维数组进行排序,具体可以参考http://in2.php.net/manual/en/function.array-multisort.php


我想对一个复杂的数组进行排序,但不知道如何做。我已经阅读了说明书,但没有找到适用于这样复杂数组的好例子。 - Anuj Jain
2个回答

2
最坏的情况下,您可以自己编写排序函数并使用 usort。实际上,它就是为这些事情设计的。
在您的情况下,您将传递 $arr[$i]['countries'] 并使比较函数基于 $arr['growth'] 进行排序。

0

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

/**
 * 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' ) );
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接