2014-12-31 23 views
1

我分揀使用的值按字母順序的陣列,所述陣列是如下:排序使用值字母字符串數組,並考慮值的情況下,在PHP

Array ( 
[0] => test This 
[1] => This test 
[2] => again this test 
[3] => test again this 
[4] => this test again 
[5] => Dallas University Texas 
[6] => Texas Dallas University 
[7] => University Texas Dallas 
[8] => dallas University Texas 
[9] => Texas dallas University 
[10] => University Texas dallas 
[11] => Johnson Johnson 
[12] => Johnson Johnson 
) 

預期輸出時,我的排序應該儘可能如下:

again this test 
dallas University Texas 
Dallas University Texas 
Johnson Johnson 
test again this 
test This 
this test again 
Texas dallas University 
Texas Dallas University 
This test 
University Texas dallas 
University Texas Dallas 

我的代碼如下:
比較

function compareValues($a,$b) { 
    if ($a == $b) { 
    return 0; 
    } 
    return ($a < $b) ? -1 : 1; 
} 
function transform($input){ 
    return usort($input,array($input,"compareValues")); 
} 
print_r($transform($input)); 

我的當前輸出不能按字母順序排列所有值並且不區分大小寫,它只能生成部分有序的數組。它應該是比較器功能有一些故障。

回答

0

我最終使用usort()對該數組進行排序。我有以下類與構成算法的方法:

class Alphabetizer extends AbstComposite{ 

    private $input = array(); 
    private $output = array(); 
function Alphabetizer($input){ 
    $this->input = $input; 
    $this->transform(); 
    $this->output(); 
} 
    static function checkcase($string) { 

     if(preg_match('/[A-Z]/', $string)){ 
      return 1; 
     }elseif(preg_match('/[a-z]/', $string)){ 
      return 0; 
     }else{ 
      return "stupid"; 
     } 
    } 
    static function caseorder($a,$b){ 
     $a = preg_replace('/\s+/','',trim($a)); 
     $b = preg_replace('/\s+/','',trim($b)); 
     //checking the case ordering of words 
     $length= (count($a) < count($b)) ? count($a) :count($b); 
     for($i=0;$i<=$length;$i++){ 
       if(strcasecmp($a[$i],$b[$i] == 0)){ 
        if((Alphabetizer::checkcase($a[$i]) ==0) && Alphabetizer::checkcase($b[$i]) ==1){ 
         return -1; 
        }elseif(Alphabetizer::checkcase($a[$i]==1) && Alphabetizer::checkcase($b[$i] ==0)){ 
         return +1; 
        }else{ 
         return 0; 
        } 
       }return 0; 



     } 
    } 
    //works well for alphabetical ordering of sentences 
    static function letterorder($a,$b){ 
     $a = preg_replace('/\s+/','',trim($a)); 
     $b = preg_replace('/\s+/','',trim($b)); 
     $length= (count($a) < count($b)) ? count($a) :count($b); 
     for($i=0;$i<=$length;$i++){ 
      if(strtolower($a[$i]) < strtolower($b[$i])){ 
       return -1; 
      }elseif(strtolower($a[$i]) > strtolower($b[$i])){ 
       return +1; 
      }else{ 
       return 0; 
      } 

     } 

    } 
    static function alphaorder($a,$b){ 
     $a = preg_replace('/\s+/','',trim($a)); 
     $b = preg_replace('/\s+/','',trim($b)); 
     $length= (count($a) < count($b)) ? count($a) :count($b); 
     for($i=0;$i<=$length;$i++){ 
      if(strtolower($a[$i]) > strtolower($b[$i])){ 
       return 1; 
      }elseif(strtolower($a[$i]) < strtolower($b[$i])){ 
       return -1; 
      } 


     } 
    } 


    static function comparator($a,$b){ 
     $a = preg_replace('/\s+/','',trim($a)); 
     $b = preg_replace('/\s+/','',trim($b)); 
     $return_value=0; 
    if(($return_value=Alphabetizer::letterorder($a,$b))==0){ 
     if(($return_value=Alphabetizer::caseorder($a,$b))== 0){ 
      if(($return_value=Alphabetizer::alphaorder($a,$b)) == 0){ 
       if(strlen($a) < strlen($b)) { 
       return 1; 
       }else{ 
        return 0; 
       } 
      }else{return$return_value;} 
     }else{ return $return_value;} 

    }else{ 
     return $return_value; 
    } 

    } 


    public function transform(){ 
    usort($this->input,array(&$this,"Alphabetizer::comparator")); 

    } 
    public function output(){ 
     return array_unique($this->input); 
    } 

} 
2

使用PHP自身的功能,那就是

sort(array);,這裏是一個鏈接,你的幫助http://www.w3schools.com/php/php_arrays_sort.asp

,或者如果你想按字母順序排序,然後使用

natcasesort(array)這裏是鏈接http://php.net/manual/en/function.natcasesort.php

+0

可以按字母順序排序以及情況?@ agha-umair-ahmed – leboMagma

+0

不會照顧案例,因爲你想 – madforstrength

+0

我試圖asort(),但它並不適合我madforstrength – leboMagma

0

您需要使用natcasesort(array);

我得到這個結果你的陣列上應用natcasesort:

natcasesort給出了下面的輸出:

Array 
(
    [2] => again this test 
    [8] => dallas University Texas 
    [5] => Dallas University Texas 
    [11] => Johnson Johnson 
    [12] => Johnson Johnson 
    [3] => test again this 
    [0] => test This 
    [9] => Texas dallas University 
    [6] => Texas Dallas University 
    [1] => This test 
    [4] => this test again 
    [10] => University Texas dallas 
    [7] => University Texas Dallas 
) 

natsort()提供以下的輸出:

Array 
(
    [5] => Dallas University Texas 
    [11] => Johnson Johnson 
    [12] => Johnson Johnson 
    [6] => Texas Dallas University 
    [9] => Texas dallas University 
    [1] => This test 
    [7] => University Texas Dallas 
    [10] => University Texas dallas 
    [2] => again this test 
    [8] => dallas University Texas 
    [0] => test This 
    [3] => test again this 
    [4] => this test again 
) 
+0

這不能按字母順序排列所有值@madforstrength – leboMagma

+0

我在你的數組上應用了natcasesort並不是你想要的結果嗎? – madforstrength

+0

沒有。我希望指數[4]來之前[9],因爲小案件t應該在所有T @madforstrength – leboMagma

0

試試這個...

 <?php 
     $cars = array("test This", "This test", "again this test","test again this" 
,"this test again","Dallas University Texas","Texas Dallas University","University Texas Dallas","dallas University Texas","Texas dallas University","University Texas dallas", 
"Johnson Johnson","Johnson Johnson"); 
     arsort($cars); 
     natcasesort($cars); 
     print_r($cars); 
     ?> 

結果:

 Array ([2] => again this test [8] => dallas University Texas 
[5] => Dallas University Texas [12] => Johnson Johnson [11] => Johnson Johnson 
[3] => test again this [0] => test This [6] => Texas Dallas University 
[9] => Texas dallas University [1] => This test [4] => this test again 
[7] => University Texas Dallas [10] => University Texas dallas) 

參考鏈接: http://php.net/manual/en/array.sorting.php

+0

的情況,因此無法按字母順序正確排序所有值,它與案例很好@deena – leboMagma

+0

@leboMagma我更新我的答案檢查這一點。 –

+0

這就是我想要的數組([2] =>再次測試[8] =>達拉斯大學得克薩斯州[5] =>達拉斯大學得克薩斯州[11] =>約翰遜約翰遜[12] =>約翰遜約翰遜[3] = >再次測試這個[0] =>測試這個[4] =>這個測試再次[9] =>德州達拉斯大學[6] =>德州達拉斯大學[1] =>這個測試[10] => [7] =>德克薩斯州達拉斯大學)@deena – leboMagma

相關問題