2015-09-02 20 views
0

這裏是陣列,排序一個3電平多維陣列由值PHP

array(
[0] => Array 
     (
      [IdRedeemProduct] => Item-A 
      [RedeemOptions] => Array 
       (
        [0] => Array 
         (
          [Points] => 1000 
         ) 

        [1] => Array 
         (
          [Points] => 2000 
         ) 

        [2] => Array 
         (
          [Points] => 43000 
         ) 

       ) 

      [ProductType] => 1 
     ) 
[1] => Array 
     (
      [IdRedeemProduct] => Item-B 
      [RedeemOptions] => Array 
       (
        [0] => Array 
         (
          [Points] => 6200 
         ) 

        [1] => Array 
         (
          [Points] => 53000 
         ) 

       ) 

      [ProductType] => 1 
     ) 
) 

大多數usort示例僅級別2維陣列。我找不到任何3級的例子。

在這種情況下,我想排序最先顯示的最小點。項目A將是第一個,項目B將是第二個。

回答

0

試試這個:

function sort_2d_desc($array, $key) { 

    usort($array, function($a, $b) use ($key) { 
     return strnatcasecmp($b[$key], $a[$key]); 
    }); 

    return $array; 
    } 

     $a = []; 
    foreach($arr as $key => $val){ 
     $a[$key] = $this->sort_2d_desc($val['RedeemOptions'], 'Points'); 
    } 

    $newArr = []; 
    foreach($arr as $key => $val){ 

     $newArr[] = ['IdRedeemProduct' => $val['IdRedeemProduct'], 'RedeemOptions' => $a, 'ProductType' => $val['ProductType']]; 
    } 



    print_r($newArr); 
+0

回報ASORT()預計參數1爲陣,空給出。 :( –

+0

你能再試一次嗎? – aldrin27

+0

這將只返回鍵而不是值..輸出:陣列([0] => 1 [1] => 1) –

0
foreach ($filteredResults as $key => $row) 
        { 
         foreach ($row['RedeemOptions'] as $key2 => $option) { 
          $vc_array_name[$key] = $option['Points']; 
         } 
        } 

array_multisort($vc_array_name, SORT_ASC, $filteredResults); 

這是工作......