我按字母顺序使用值对数组进行排序,数组如下:
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 :(得分:2)
使用
的php本机功能 sort(array);
以下是您的帮助http://www.w3schools.com/php/php_arrays_sort.asp
或者如果您想按字母顺序排序,请使用
natcasesort(array)
这里是链接http://php.net/manual/en/function.natcasesort.php
答案 1 :(得分: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
)
答案 2 :(得分: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 )
答案 3 :(得分: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);
}
}