php多维数组排序问题

时间:2012-07-17 14:58:28

标签: php sorting multidimensional-array

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

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

所示

2 个答案:

答案 0 :(得分:2)

最糟糕的情况是,您可以创建自己的排序功能并使用usort 它实际上是为这些事物而设计的。

在您的情况下,您将传递$arr[$i]['countries']并根据$arr['growth']进行比较功能排序。

答案 1 :(得分: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' ) );
}