多维数组按价格升序和降序排序

时间:2014-01-24 07:59:32

标签: php arrays sorting multidimensional-array

这是我得到的以下数组......!我该怎么办呢。

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [price] => 29.99
                    [params] => 
                    [text] => demotext
                )

            [1] => Array
                (
                    [price] => 22.40
                    [params] => 
                    [text] => demotext
                )

            [2] => Array
                (
                    [price] => 12.95
                    [params] => 
                    [text] => demotext
                )

            [3] => Array
                (
                    [price] => 9.60
                    [params] => 
                    [text] => demotext
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [price] => 8.16
                    [params] => 
                    [text] => demotext
                )

            [1] => Array
                (
                    [price] => 7.66
                    [params] => 
                    [text] => demotext
                )

            [2] => Array
                (
                    [price] => 7.19
                    [params] => 
                    [text] => demotext
                )

            [3] => Array
                (
                    [price] => 7.14
                    [params] => 
                    [text] => demotext
                )

        )

正如您在索引2中看到的那样,数组未被排序beacuse 5.10应该在索引[1]上,4.79应该在索引[2]上

[2] => Array
        (
            [0] => Array
                (
                    [price] => 5.99
                    [params] => 
                    [text] => demotext
                )

            [1] => Array
                (
                    [price] => 4.79
                    [params] => 
                    [text] => demotext
                )

            [2] => Array
                (
                    [price] => 5.10
                    [params] => 
                    [text] => demotext
                )

            [3] => Array
                (
                    [price] => 4.20
                    [params] => 
                    [text] => demotext
                )

        )
    [3] => Array
        (
            [0] => Array
                (
                    [price] => 4.08
                    [params] => 
                    [text] => demotext
                )

            [1] => Array
                (
                    [price] => 4.00
                    [params] => 
                    [text] => demotext
                )

            [2] => Array
                (
                    [price] => 3.20
                    [params] => 
                    [text] => demotext
                )

            [3] => Array
                (
                    [price] => 3.19
                    [params] => 
                    [text] => demotext
                )

        )

    [4] => Array
        (
            [0] => Array
                (
                    [price] => 2.86
                    [params] => 
                    [text] => demotext
                )

            [1] => Array
                (
                    [price] => 3.58
                    [params] => 
                    [text] => demotext
                )

            [2] => Array
                (
                    [price] => 2.82
                    [params] => 
                    [text] => demotext
                )

            [3] => Array
                (
                    [price] => 2.90
                    [params] => 
                    [text] => demotext
                )

        )

)

4 个答案:

答案 0 :(得分:1)

我认为你可以使用array_multisort() http://ch2.php.net/array_multisort

答案 1 :(得分:1)

假设$ arr是你的数组然后尝试:

foreach($arr as &$ar){
  foreach($ar as $key=>$r){
    $price[$key] = $r['price'];
    $params[$key] = $r['params'];
    $text[$key] = $r['text'];
  }
 array_multisort($price, SORT_DESC, $params, SORT_REGULAR, $text,SORT_REGULAR,$ar);
}

请参阅演示here

答案 2 :(得分:0)

尝试使用array_multisort函数http://pl1.php.net/array_multisort

答案 3 :(得分:0)

这是我在大惊小怪之后排序的,将多维数组打破为One Array,然后根据价格对其进行排序。

$array = your array;
// merging multi-dimension array into one array.
$result = array_merge_recursive($array[0],$array[1],$array[2],$array[3],$array[4]);
// now Sort array according to price
array_multisort($result, SORT_ASC);
foreach ($result as $key => $val) {
   foreach ($val as $new) { 
   }
echo  $new;
  }