2012-08-09 30 views
2

此問題的跟進問題來自'This question'。 codeigniter代碼返回數據庫記錄集,如下所示。在一定條件的基礎上使用usort對多維數組進行排序

Array 
(
    [0] => stdClass Object 
    (
     [user_id] => NLK32439 
     [first_name] => sdn 
     [last_name] => hf]zL 
     [email_address] => [email protected] 
     [mobile_number] => 9841349349 
     [description] => g]kfn 
     [date_joined] => 09-AUG-12 
     [status] => 1 
     [username] => user1 
     [userpassword] => 691f9298642af07c2d6ea8fef56074201e077b34 
    ) 

    [1] => stdClass Object 
    (
     [user_id] => NLK94358 
     [first_name] => alag 
     [last_name] => k|wfg 
     [email_address] => [email protected] 
     [mobile_number] => 823472384723 
     [description] => g]kfn 
     [date_joined] => 09-AUG-12 
     [status] => 1 
     [username] => user2 
     [userpassword] => 691f9298642af07c2d6ea8fef56074201e077b34 
    ) 

    [2] => stdClass Object 
    (
     [user_id] => NLK32437 
     [first_name] => ;lag 
     [last_name] => %]qL 
     [email_address] => [email protected] 
     [mobile_number] => 9851112412 
     [description] => g]kfn 
     [date_joined] => 08-AUG-12 
     [status] => 1 
     [username] => user3 
     [userpassword] => 691f9298642af07c2d6ea8fef56074201e077b34 
    ) 

    [3] => stdClass Object 
    (
     [user_id] => NLK32435 
     [first_name] => clgn 
     [last_name] => zdf{ 
     [email_address] => [email protected] 
     [mobile_number] => 984134354 
     [description] => g]kfn 
     [date_joined] => 08-AUG-12 
     [status] => 1 
     [username] => user4 
     [userpassword] => 0e025eade868b4b481f41ff7449bc1967261e170 
    ) 

) 

我想要做的就是根據一定的條件按「first_name」排序數組。我的PHP代碼到目前爲止是 -

<?php 
usort($array, function ($a, $b) { 
    static $order = array('c', 's','a',';', 'L'); 
    return array_search($a->first_name, $order) - array_search($b->first_name, $order); 
}); 
?> 

我打算做的是排序的陣列,使得如first_name與「C」 begining至上,以「S」排第二,用「A」來第三和與' ;」排在第四位。

但上面的代碼沒有按預期工作。它返回的記錄的順序:

's','a',';','c' 

應該序列

'c', 's','a',';' 

我爲使用自定義字體天城文中返回,所以在鍵盤的每個albhabet表示天城文特定字符。所以在devnagari中,c首先,第二個等等。

任何幫助將不勝感激。提前致謝

回答

2

這是因爲使用array_search只會在字符串完全匹配數組中的某個字符串時返回。

$array = array('a','b','c'); 
var_dump(array_search('blue',$array)) 
// This will output 
bool(false) 

所以,如果你願意,你可以試試這個:

<?php 
usort($array, function ($a, $b) { 
static $order = array('c', 's','a',';', 'L'); 
return array_search(substr($a->first_name,0,1), $order) - array_search(substr($b->first_name,0,1), $order); 
}); 
?> 

希望這有助於。

更新:對於你對你的評論的問題,我有一個想法。請看一下。

usort($array, function($a, $b) { 
    //example for the orders 
    static $orders = array (';l', 'c', 's', 'a', ';', 'L'); 

    //variables to count the point for $a and $b (it is equal at first) 
    $pointA = 1; 
    $pointB = 1; 

    //also variables to mark if $a or $b are matched on one of the orders 
    $isFoundA = false; 
    $isFoundB = false; 

    //iterate foreach orders 
    $i = 0; //this is something to add to the point, the more earlier the first_name found in the order, the less point will be added 
    foreach ($orders as $order) { 
     //if $a->first_name is still not found in orders and it has been just founded 
     if (!$isFoundA && strpos($a->first_name,$order) === 0) { 
      $pointA += $i; 
      $isFoundA = true; //we don't need to check this again since we had found it 
     } 
     //if $b->first_name is still not found in orders and it has been just founded 
     if (!isFoundB && strpos($b->first_name,$order) === 0) { 
      $pointB += $i; 
      $isFoundB = true; //the same as above, we don't need to check it again 
     } 
     $i++; 
    } 
    //after iterate in orders, we check the points for $a and $b 
    if ($pointA == $pointB) return 0; 
    else return ($pointA < $pointB) ? -1 : 1; 
}); 

現在訂單沒有字符限制。您可以指定類似static $orders = array('abc','a',';');static $orders = array('abcdef', 'b', 'z', ';', 'c[');請讓我知道它是否適用於您。

+0

是的,我已經知道了,謝謝你的答覆。但遇到了另一個問題。有時兩個字母代表Devnagari中的一個字符,就像s代表devnagari中的一個字符一樣。無論如何要保持這一點,例如,如果我們有這樣的$ order數組,我想檢查第一個字符和前兩個字符,那麼可能的解決方案是什麼? static $ order = array('c','s','s]','a',';','L'); – WatsMyName 2012-08-09 10:19:15

+1

@Sabin我已經更新了我的答案。 – 2012-08-09 18:25:56

+0

非常感謝代碼,但代碼看起來不像預期的那樣工作。我有什麼是 static $ orders = array('c','s','s'「,'a',';','L'); 結果應該與上述數組的順序相同,但其顯示順序爲「c」,「a」,「s」,「;」,「s」 – WatsMyName 2012-08-10 03:49:32

相關問題