2014-03-26 41 views
0

我有這樣PHP多排序通過使用外部陣列

Array 
(
    [0] => Array 
     (
      [id] => 21 
      [time] => FOUR_DAY 
      [zones] => 25 
     ) 

    [1] => Array 
     (
      [id] => 20 
      [time] => THREE_DAY 
      [zones] => 2 
     ) 
    [2] => Array 
     (
      [id] => 22 
      [time] => ONE_DAY 
      [zones] => 2 
     ) 
    [3] => Array 
     (
      [id] => 24 
      [time] => ONE_DAY 
      [zones] => 2 
     ) 
) 

陣列我所需要的是

1-排序由「時間」字段不同條件下,採用

$time = array('ONE_DAY','TWO_DAYS','THREE_DAYS','FOUR_DAYS','FIVE_DAYS'); 

2-然後按ASC順序按'區域'對上述結果進行排序。

預期輸出:

Array 
(
    [0] => Array 
     (
      [id] => 22 
      [time] => ONE_DAY 
      [zones] => 2 
     ) 

    [1] => Array 
     (
      [id] => 24 
      [time] => ONE_DAY 
      [zones] => 2 
     ) 
    [2] => Array 
     (
      [id] => 20 
      [time] => THREE_DAY 
      [zones] => 2 
     ) 
    [3] => Array 
     (
      [id] => 21 
      [time] => FOUR_DAY 
      [zones] => 25 
     ) 
) 

請幫我一個不錯的選擇。我曾嘗試使用usort作爲'時間'字段和multi_sort函數。但停滯不前。 在此先感謝!

+1

這很棒,你用usort試過。但是顯示你已經嘗試過的代碼。然後我們會嘗試修復它。 –

+0

什麼決定了您想要的結果中索引0和1的項目的排序順序? 「id」是第三種排序標準,還是您關心id上的排序順序可能無法預測?你有沒有考慮將'ONE_DAY','TWO_DAY'等等定義爲常量,以便更容易進行排序? –

+0

@dragoste:我按照我的要求使用這段代碼進行'時間'排序。
usort($ myArray,function($ a,$ b)use($ time){pos_a = array_search($ a ['time'],$ time); \t \t $ pos_b = array_search($ b [''時間'],$ time); \t \t return $ pos_a - $ pos_b; \t}); –

回答

0

感謝所有細節usort!我終於找到了解決問題的辦法。
作爲$時間是預先定義的值,

替換

$time = array('ONE_DAY','TWO_DAYS','THREE_DAYS','FOUR_DAYS','FIVE_DAYS');

AS

$time = array(1=>'ONE_DAY',2=>'TWO_DAYS',3=>'THREE_DAYS',4=>'FOUR_DAYS',5=>'FIVE_DAYS');

,並用於在array_multisort,而不是usort

 
    `foreach($myArray as $key=>$response){`enter code here` 
     $sortNumber = array_search($response['time'],$time); 
     $myArray [$key]['numericTimes'] = ($sortNumber)?$sortNumber:"NULL"; 
     $sortByTime[$key] = array_search($response['time'],$time); 
     $sortBtZone[$key] = $response['zones']; 
    }`  
array_multisort($sortByTime, SORT_ASC, $sortBtZone, SORT_ASC, $myArray); 

希望這有助於某人在某個點!
謝謝!

0

您應該使用usort執行此任務。

試着這麼做:

$sorted = usort($notSorted, function($a, $b) { 
    //array of times to compare 
    $time = array('ONE_DAY','TWO_DAYS','THREE_DAYS','FOUR_DAYS','FIVE_DAYS'); 

    //check index of time in $times array 
    $aKey = array_search($a['time'], $times); 
    $bKey = array_search($b['time'], $times); 

    //if the same - then compare by zones; 
    if($aKey == $bkey) { 
     return $a['zones'] - $b['zones']; 
    //else compare by time's indexes 
    } else { 
     return ($aKey-$bKey); 
    } 
}); 

不受顯示測試的想法。

檢查PHP手冊

0
$order = array("ONE_DAY","TWO_DAY","THREE_DAY","FOUR_DAY"); 

    $test[4] =array("id"=>21,"time"=>"FOUR_DAY","zones"=>25); 
    $test[1] =array("id"=>20,"time"=>"THREE_DAY","zones"=> 2); 
    $test[2] =array("id"=>22,"time"=>"ONE_DAY","zones"=>2); 
    $test[3] =array("id"=>24,"time"=>"ONE_DAY","zones"=>2);  

    $newarray=array(); 
    foreach($test as $kk=>$t){ 
    $newarray[$kk]=$t["time"];  
    }  

    $final=array();  
    foreach($order as $key=>$f){ 
    $mykey=array_keys($newarray,$f); 


    if(is_array($mykey)){ 
     foreach($mykey as $key){ 
     $final[]=$test[$key]; 
     } 
    } 

    } 

    echo "<pre>"; 
    print_r($final); 
    echo "</pre>";