排序多维数组不起作用

时间:2014-09-15 22:39:48

标签: php arrays sorting multidimensional-array

我有一个数组变量$ data,我试图按字母顺序排序。我正在经历一个foreach循环并尝试排序密钥。

中间的数组未进行排序以匹配其他数组。 Yelp应该是最后一个,而DealerRater是最后一个。

我正在尝试这个:

foreach ($data as $key=>$value) {
   ksort($key);
}

我的$ data数组:

Array
(
[1] => Array
    (
        [Cars.com] => Array
            (
                [rooftop_id] => 1
                [rooftop_name] => Norm Reeves Honda - Cerritos
                [name] => Cars.com
                [review_site_id] => 30
                [review_count] => 289
                [review_average] => 4.80
            )

        [Dealer Rater] => Array
            (
                [rooftop_id] => 1
                [rooftop_name] => Norm Reeves Honda - Cerritos
                [name] => Dealer Rater
                [review_site_id] => 10
                [review_count] => 1231
                [review_average] => 4.90
            )

        [Google+ Local] => Array
            (
                [rooftop_id] => 1
                [rooftop_name] => Norm Reeves Honda - Cerritos
                [name] => Google+ Local
                [review_site_id] => 31
                [review_count] => 556
                [review_average] => 4.80
            )

        [Yelp] => Array
            (
                [rooftop_id] => 1
                [rooftop_name] => Norm Reeves Honda - Cerritos
                [name] => Yelp
                [review_site_id] => 29
                [review_count] => 423
                [review_average] => 3.50
            )

    )

[45] => Array
    (
        [Cars.com] => Array
            (
                [rooftop_id] => 45
                [rooftop_name] => Leith Volkswagen of Raleigh
                [name] => Cars.com
                [review_site_id] => 30
                [review_count] => 95
                [review_average] => 4.90
            )

        [Google+ Local] => Array
            (
                [rooftop_id] => 45
                [rooftop_name] => Leith Volkswagen of Raleigh
                [name] => Google+ Local
                [review_site_id] => 31
                [review_count] => 21
                [review_average] => 4.80
            )

        [Yelp] => Array
            (
                [rooftop_id] => 45
                [rooftop_name] => Leith Volkswagen of Raleigh
                [name] => Yelp
                [review_site_id] => 29
                [review_count] => 3
                [review_average] => 1.50
            )

        [Dealer Rater] => Array
            (
                [rooftop_id] => 45
                [rooftop_name] => Leith Volkswagen of Raleigh
                [name] => Dealer Rater
                [review_site_id] => 10
                [review_count] => 0
                [review_average] => 0
            )

    )

[56] => Array
    (
        [Cars.com] => Array
            (
                [rooftop_id] => 56
                [rooftop_name] => Wilde Jaguar Of Sarasota
                [name] => Cars.com
                [review_site_id] => 30
                [review_count] => 34
                [review_average] => 4.70
            )

        [Dealer Rater] => Array
            (
                [rooftop_id] => 56
                [rooftop_name] => Wilde Jaguar Of Sarasota
                [name] => Dealer Rater
                [review_site_id] => 10
                [review_count] => 271
                [review_average] => 4.90
            )

        [Google+ Local] => Array
            (
                [rooftop_id] => 56
                [rooftop_name] => Wilde Jaguar Of Sarasota
                [name] => Google+ Local
                [review_site_id] => 31
                [review_count] => 31
                [review_average] => 4.70
            )

        [Yelp] => Array
            (
                [rooftop_id] => 56
                [rooftop_name] => Wilde Jaguar Of Sarasota
                [name] => Yelp
                [review_site_id] => 29
                [review_count] => 1
                [review_average] => 1.00
            )

    )
)

1 个答案:

答案 0 :(得分:0)

你正在排序错误的值。这样做:

foreach ($data as $key=>$value) {
   ksort($value);
}

$key是主数组的索引。 $value是要排序的数组元素。如果你更逻辑地命名你的变量会更有意义,比如:

foreach ($data as $index=>$element) {
   ksort($element);
}

或简称:

foreach ($data as $element) {
   ksort($element);
}