2014-01-24 117 views
0

这是以下数组我正在...!我怎么能把它分类。多维数组按价格排序升序和降序

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阵列未排序怎么一回事,因为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 
       ) 

     ) 

) 
+0

你到目前为止尝试过什么? –

+0

可能的重复在这里:http://stackoverflow.com/questions/2699086/sort-multi-dimensional-array-by-value – Atiris

+0

你期望的结果是什么? –

回答

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

+0

感谢Noupal.M的帮助......! –

0

这是我后大做文章分类,打破多维数组转换成一个阵列,然后根据价格排列它。

$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; 
    }