2014-01-24 164 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; 
    }