为什么usort()结果为TRUE,而不是排序数组?

时间:2012-01-17 17:03:34

标签: php sorting compare usort

我需要根据另一个对数组进行排序,我昨天在这里问过并得到了一些启发性答案:Sort an array by using the same order of another one

现在我正在尝试通过使用$ array_sorted中使用的顺序对$ array1进行排序来实现类似的任务。 两者之间存在一些差异,所以我尝试编写一个合适的比较函数。

s 表示“同一套装”, o 表示“不同套装”。所以“AhJs”应该变成“AJo”来与$ array_sorted进行比较。

这是我实际写的:

function cmp($a, $b) {

    $array_sorted = array_flip(array("AA","KK","QQ","JJ","AKs","AQs","TT","AKo","AJs","KQs","99","ATs","AQo","KJs","88","QJs","KTs","A9s","AJo","QTs","KQo","77","JTs","A8s","K9s","ATo","A7s","A5s","A7o","KJo","66","T9s","A4s","Q9s","J9s","QJo","A6s","55","A3s","K8s","KTo","98s","T8s","K7s","A2s","87s","QTo","Q8s","44","A9o","J8s","76s","JTo","97s","K6s","K5s","K4s","T7s","Q7s","K9o","65s","T9o","86s","A8o","J7s","33","54s","Q6s","K3s","Q9o","75s","22","J9o","64s","Q5s","K2s","96s","Q3s","J8o","98o","T8o","97o","A7o","T7o","Q4s","Q8o","J5s","T6o","75o","J4s","74s","K8o","86o","53s","K7o","63s","J6s","85o","T6s","76o","A6o","T2o","95s","84o","62o","T5s","95o","A5o","Q7o","T5o","87o","83o","65o","Q2s","94o","74o","54o","A4o","T4o","82o","64o","42o","J7o","93o","85s","73o","53o","T3o","63o","K6o","J6o","96o","92o","72o","52o","Q4o","K5o","J5o","43s","Q3o","43o","K4o","J4o","T4s","Q6o","Q2o","J3s","J3o","T3s","A3o","Q5o","J2o","84s","82s","42s","93s","73s","K3o","J2s","92s","52s","K2o","T2s","62s","32o","A2o","83s","94s","72s","32s"));
    // If $a is AA and $b is J9s then
    // $array_sorted[ 'AA' ] - $array_sorted[ 'J9s' ]
    // will evaluate to a negative number, telling PHP that $a (AA)
    // is "smaller" than $b (J9s) and so goes first.
    $a1 = substr($a,0,1);
    $a1s = substr($a,1,1);
    $a2 = substr($a,2,1);
    $a2s = substr($a,3,1);
    $b1 = substr($b,0,1);
    $b1s = substr($b,1,1);
    $b2 = substr($b,2,1);
    $b2s = substr($b,3,1);
    if ($a1s == $a2s) {
        $a = $a1.$a2."s";
    }
    else if ($a1 !== $a2s) {
        if ($a1 !== $a2) {
            $a = $a1.$a2."o";
        }
        else {
            $a = $a1.$a2;
        }
    }

    if ($b1s == $b2s) {
        $b = $b1.$b2."s";
    }
    else if ($b1 !== $b2s) {
        if ($b1 !== $b2) {
            $b = $b1.$b2."o";
        }
        else {
            $b = $b1.$b2;
        }
    }
    //echo $a." - ".$b;
    return $array_sorted[$a] - $array_sorted[$b];
  }

$array1 = array("AhAc","AhAd","AhAs","AhKh","AhKc","AhKd","AhKs","AhQh","AhQc","AhQd","AhQs","AhJh","AhJc","AhJd","AhJs","AhTh","AhTc","AhTd","AhTs","Ah9h","Ah9c","Ah9d","Ah9s","Ah8h","Ah8c","Ah8d","Ah8s","Ah7h","Ah7c","Ah7d","Ah7s","Ah6h","Ah6c","Ah6d","Ah6s","Ah5h","Ah5c","Ah5d","Ah5s","Ah4h","Ah4c","Ah4d","Ah4s","Ah3h","Ah3c","Ah3d","Ah3s","Ah2h","Ah2c","Ah2d","Ah2s","AcAd","AcAs","AcKh","AcKc","AcKd","AcKs","AcQh","AcQc","AcQd","AcQs","AcJh","AcJc","AcJd","AcJs","AcTh","AcTc","AcTd","AcTs","Ac9h","Ac9c","Ac9d","Ac9s","Ac8h","Ac8c","Ac8d","Ac8s","Ac7h","Ac7c","Ac7d","Ac7s","Ac6h","Ac6c","Ac6d","Ac6s","Ac5h","Ac5c","Ac5d","Ac5s","Ac4h","Ac4c","Ac4d","Ac4s","Ac3h","Ac3c","Ac3d","Ac3s","Ac2h","Ac2c","Ac2d","Ac2s","AdAs","AdKh","AdKc","AdKd","AdKs","AdQh","AdQc","AdQd","AdQs","AdJh","AdJc","AdJd","AdJs","AdTh","AdTc","AdTd","AdTs","Ad9h","Ad9c","Ad9d","Ad9s","Ad8h","Ad8c","Ad8d","Ad8s","Ad7h","Ad7c","Ad7d","Ad7s","Ad6h","Ad6c","Ad6d","Ad6s","Ad5h","Ad5c","Ad5d","Ad5s","Ad4h","Ad4c","Ad4d","Ad4s","Ad3h","Ad3c","Ad3d","Ad3s","Ad2h","Ad2c","Ad2d","Ad2s","AsKh","AsKc","AsKd","AsKs","AsQh","AsQc","AsQd","AsQs","AsJh","AsJc","AsJd","AsJs","AsTh","AsTc","AsTd","AsTs","As9h","As9c","As9d","As9s","As8h","As8c","As8d","As8s","As7h","As7c","As7d","As7s","As6h","As6c","As6d","As6s","As5h","As5c","As5d","As5s","As4h","As4c","As4d","As4s","As3h","As3c","As3d","As3s","As2h","As2c","As2d","As2s","KhKc","KhKd","KhKs","KhQh","KhQc","KhQd","KhQs","KhJh","KhJc","KhJd","KhJs","KhTh","KhTc","KhTd","KhTs","Kh9h","Kh9c","Kh9d","Kh9s","Kh8h","Kh8c","Kh8d","Kh8s","Kh7h","Kh7c","Kh7d","Kh7s","Kh6h","Kh6c","Kh6d","Kh6s","Kh5h","Kh5c","Kh5d","Kh5s","Kh4h","Kh4c","Kh4d","Kh4s","Kh3h","Kh3c","Kh3d","Kh3s","Kh2h","Kh2c","Kh2d","Kh2s","KcKd","KcKs","KcQh","KcQc","KcQd","KcQs","KcJh","KcJc","KcJd","KcJs","KcTh","KcTc","KcTd","KcTs","Kc9h","Kc9c","Kc9d","Kc9s","Kc8h","Kc8c","Kc8d","Kc8s","Kc7h","Kc7c","Kc7d","Kc7s","Kc6h","Kc6c","Kc6d","Kc6s","Kc5h","Kc5c","Kc5d","Kc5s","Kc4h","Kc4c","Kc4d","Kc4s","Kc3h","Kc3c","Kc3d","Kc3s","Kc2h","Kc2c","Kc2d","Kc2s","KdKs","KdQh","KdQc","KdQd","KdQs","KdJh","KdJc","KdJd","KdJs","KdTh","KdTc","KdTd","KdTs","Kd9h","Kd9c","Kd9d","Kd9s","Kd8h","Kd8c","Kd8d","Kd8s","Kd7h","Kd7c","Kd7d","Kd7s","Kd6h","Kd6c","Kd6d","Kd6s","Kd5h","Kd5c","Kd5d","Kd5s","Kd4h","Kd4c","Kd4d","Kd4s","Kd3h","Kd3c","Kd3d","Kd3s","Kd2h","Kd2c","Kd2d","Kd2s","KsQh","KsQc","KsQd","KsQs","KsJh","KsJc","KsJd","KsJs","KsTh","KsTc","KsTd","KsTs","Ks9h","Ks9c","Ks9d","Ks9s","Ks8h","Ks8c","Ks8d","Ks8s","Ks7h","Ks7c","Ks7d","Ks7s","Ks6h","Ks6c","Ks6d","Ks6s","Ks5h","Ks5c","Ks5d","Ks5s","Ks4h","Ks4c","Ks4d","Ks4s","Ks3h","Ks3c","Ks3d","Ks3s","Ks2h","Ks2c","Ks2d","Ks2s","QhQc","QhQd","QhQs","QhJh","QhJc","QhJd","QhJs","QhTh","QhTc","QhTd","QhTs","Qh9h","Qh9c","Qh9d","Qh9s","Qh8h","Qh8c","Qh8d","Qh8s","Qh7h","Qh7c","Qh7d","Qh7s","Qh6h","Qh6c","Qh6d","Qh6s","Qh5h","Qh5c","Qh5d","Qh5s","Qh4h","Qh4c","Qh4d","Qh4s","Qh3h","Qh3c","Qh3d","Qh3s","Qh2h","Qh2c","Qh2d","Qh2s","QcQd","QcQs","QcJh","QcJc","QcJd","QcJs","QcTh","QcTc","QcTd","QcTs","Qc9h","Qc9c","Qc9d","Qc9s","Qc8h","Qc8c","Qc8d","Qc8s","Qc7h","Qc7c","Qc7d","Qc7s","Qc6h","Qc6c","Qc6d","Qc6s","Qc5h","Qc5c","Qc5d","Qc5s","Qc4h","Qc4c","Qc4d","Qc4s","Qc3h","Qc3c","Qc3d","Qc3s","Qc2h","Qc2c","Qc2d","Qc2s","QdQs","QdJh","QdJc","QdJd","QdJs","QdTh","QdTc","QdTd","QdTs","Qd9h","Qd9c","Qd9d","Qd9s","Qd8h","Qd8c","Qd8d","Qd8s","Qd7h","Qd7c","Qd7d","Qd7s","Qd6h","Qd6c","Qd6d","Qd6s","Qd5h","Qd5c","Qd5d","Qd5s","Qd4h","Qd4c","Qd4d","Qd4s","Qd3h","Qd3c","Qd3d","Qd3s","Qd2h","Qd2c","Qd2d","Qd2s","QsJh","QsJc","QsJd","QsJs","QsTh","QsTc","QsTd","QsTs","Qs9h","Qs9c","Qs9d","Qs9s","Qs8h","Qs8c","Qs8d","Qs8s","Qs7h","Qs7c","Qs7d","Qs7s","Qs6h","Qs6c","Qs6d","Qs6s","Qs5h","Qs5c","Qs5d","Qs5s","Qs4h","Qs4c","Qs4d","Qs4s","Qs3h","Qs3c","Qs3d","Qs3s","Qs2h","Qs2c","Qs2d","Qs2s","JhJc","JhJd","JhJs","JhTh","JhTc","JhTd","JhTs","Jh9h","Jh9c","Jh9d","Jh9s","Jh8h","Jh8c","Jh8d","Jh8s","Jh7h","Jh7c","Jh7d","Jh7s","Jh6h","Jh6c","Jh6d","Jh6s","Jh5h","Jh5c","Jh5d","Jh5s","Jh4h","Jh4c","Jh4d","Jh4s","Jh3h","Jh3c","Jh3d","Jh3s","Jh2h","Jh2c","Jh2d","Jh2s","JcJd","JcJs","JcTh","JcTc","JcTd","JcTs","Jc9h","Jc9c","Jc9d","Jc9s","Jc8h","Jc8c","Jc8d","Jc8s","Jc7h","Jc7c","Jc7d","Jc7s","Jc6h","Jc6c","Jc6d","Jc6s","Jc5h","Jc5c","Jc5d","Jc5s","Jc4h","Jc4c","Jc4d","Jc4s","Jc3h","Jc3c","Jc3d","Jc3s","Jc2h","Jc2c","Jc2d","Jc2s","JdJs","JdTh","JdTc","JdTd","JdTs","Jd9h","Jd9c","Jd9d","Jd9s","Jd8h","Jd8c","Jd8d","Jd8s","Jd7h","Jd7c","Jd7d","Jd7s","Jd6h","Jd6c","Jd6d","Jd6s","Jd5h","Jd5c","Jd5d","Jd5s","Jd4h","Jd4c","Jd4d","Jd4s","Jd3h","Jd3c","Jd3d","Jd3s","Jd2h","Jd2c","Jd2d","Jd2s","JsTh","JsTc","JsTd","JsTs","Js9h","Js9c","Js9d","Js9s","Js8h","Js8c","Js8d","Js8s","Js7h","Js7c","Js7d","Js7s","Js6h","Js6c","Js6d","Js6s","Js5h","Js5c","Js5d","Js5s","Js4h","Js4c","Js4d","Js4s","Js3h","Js3c","Js3d","Js3s","Js2h","Js2c","Js2d","Js2s","ThTc","ThTd","ThTs","Th9h","Th9c","Th9d","Th9s","Th8h","Th8c","Th8d","Th8s","Th7h","Th7c","Th7d","Th7s","Th6h","Th6c","Th6d","Th6s","Th5h","Th5c","Th5d","Th5s","Th4h","Th4c","Th4d","Th4s","Th3h","Th3c","Th3d","Th3s","Th2h","Th2c","Th2d","Th2s","TcTd","TcTs","Tc9h","Tc9c","Tc9d","Tc9s","Tc8h","Tc8c","Tc8d","Tc8s","Tc7h","Tc7c","Tc7d","Tc7s","Tc6h","Tc6c","Tc6d","Tc6s","Tc5h","Tc5c","Tc5d","Tc5s","Tc4h","Tc4c","Tc4d","Tc4s","Tc3h","Tc3c","Tc3d","Tc3s","Tc2h","Tc2c","Tc2d","Tc2s","TdTs","Td9h","Td9c","Td9d","Td9s","Td8h","Td8c","Td8d","Td8s","Td7h","Td7c","Td7d","Td7s","Td6h","Td6c","Td6d","Td6s","Td5h","Td5c","Td5d","Td5s","Td4h","Td4c","Td4d","Td4s","Td3h","Td3c","Td3d","Td3s","Td2h","Td2c","Td2d","Td2s","Ts9h","Ts9c","Ts9d","Ts9s","Ts8h","Ts8c","Ts8d","Ts8s","Ts7h","Ts7c","Ts7d","Ts7s","Ts6h","Ts6c","Ts6d","Ts6s","Ts5h","Ts5c","Ts5d","Ts5s","Ts4h","Ts4c","Ts4d","Ts4s","Ts3h","Ts3c","Ts3d","Ts3s","Ts2h","Ts2c","Ts2d","Ts2s","9h9c","9h9d","9h9s","9h8h","9h8c","9h8d","9h8s","9h7h","9h7c","9h7d","9h7s","9h6h","9h6c","9h6d","9h6s","9h5h","9h5c","9h5d","9h5s","9h4h","9h4c","9h4d","9h4s","9h3h","9h3c","9h3d","9h3s","9h2h","9h2c","9h2d","9h2s","9c9d","9c9s","9c8h","9c8c","9c8d","9c8s","9c7h","9c7c","9c7d","9c7s","9c6h","9c6c","9c6d","9c6s","9c5h","9c5c","9c5d","9c5s","9c4h","9c4c","9c4d","9c4s","9c3h","9c3c","9c3d","9c3s","9c2h","9c2c","9c2d","9c2s","9d9s","9d8h","9d8c","9d8d","9d8s","9d7h","9d7c","9d7d","9d7s","9d6h","9d6c","9d6d","9d6s","9d5h","9d5c","9d5d","9d5s","9d4h","9d4c","9d4d","9d4s","9d3h","9d3c","9d3d","9d3s","9d2h","9d2c","9d2d","9d2s","9s8h","9s8c","9s8d","9s8s","9s7h","9s7c","9s7d","9s7s","9s6h","9s6c","9s6d","9s6s","9s5h","9s5c","9s5d","9s5s","9s4h","9s4c","9s4d","9s4s","9s3h","9s3c","9s3d","9s3s","9s2h","9s2c","9s2d","9s2s","8h8c","8h8d","8h8s","8h7h","8h7c","8h7d","8h7s","8h6h","8h6c","8h6d","8h6s","8h5h","8h5c","8h5d","8h5s","8h4h","8h4c","8h4d","8h4s","8h3h","8h3c","8h3d","8h3s","8h2h","8h2c","8h2d","8h2s","8c8d","8c8s","8c7h","8c7c","8c7d","8c7s","8c6h","8c6c","8c6d","8c6s","8c5h","8c5c","8c5d","8c5s","8c4h","8c4c","8c4d","8c4s","8c3h","8c3c","8c3d","8c3s","8c2h","8c2c","8c2d","8c2s","8d8s","8d7h","8d7c","8d7d","8d7s","8d6h","8d6c","8d6d","8d6s","8d5h","8d5c","8d5d","8d5s","8d4h","8d4c","8d4d","8d4s","8d3h","8d3c","8d3d","8d3s","8d2h","8d2c","8d2d","8d2s","8s7h","8s7c","8s7d","8s7s","8s6h","8s6c","8s6d","8s6s","8s5h","8s5c","8s5d","8s5s","8s4h","8s4c","8s4d","8s4s","8s3h","8s3c","8s3d","8s3s","8s2h","8s2c","8s2d","8s2s","7h7c","7h7d","7h7s","7h6h","7h6c","7h6d","7h6s","7h5h","7h5c","7h5d","7h5s","7h4h","7h4c","7h4d","7h4s","7h3h","7h3c","7h3d","7h3s","7h2h","7h2c","7h2d","7h2s","7c7d","7c7s","7c6h","7c6c","7c6d","7c6s","7c5h","7c5c","7c5d","7c5s","7c4h","7c4c","7c4d","7c4s","7c3h","7c3c","7c3d","7c3s","7c2h","7c2c","7c2d","7c2s","7d7s","7d6h","7d6c","7d6d","7d6s","7d5h","7d5c","7d5d","7d5s","7d4h","7d4c","7d4d","7d4s","7d3h","7d3c","7d3d","7d3s","7d2h","7d2c","7d2d","7d2s","7s6h","7s6c","7s6d","7s6s","7s5h","7s5c","7s5d","7s5s","7s4h","7s4c","7s4d","7s4s","7s3h","7s3c","7s3d","7s3s","7s2h","7s2c","7s2d","7s2s","6h6c","6h6d","6h6s","6h5h","6h5c","6h5d","6h5s","6h4h","6h4c","6h4d","6h4s","6h3h","6h3c","6h3d","6h3s","6h2h","6h2c","6h2d","6h2s","6c6d","6c6s","6c5h","6c5c","6c5d","6c5s","6c4h","6c4c","6c4d","6c4s","6c3h","6c3c","6c3d","6c3s","6c2h","6c2c","6c2d","6c2s","6d6s","6d5h","6d5c","6d5d","6d5s","6d4h","6d4c","6d4d","6d4s","6d3h","6d3c","6d3d","6d3s","6d2h","6d2c","6d2d","6d2s","6s5h","6s5c","6s5d","6s5s","6s4h","6s4c","6s4d","6s4s","6s3h","6s3c","6s3d","6s3s","6s2h","6s2c","6s2d","6s2s","5h5c","5h5d","5h5s","5h4h","5h4c","5h4d","5h4s","5h3h","5h3c","5h3d","5h3s","5h2h","5h2c","5h2d","5h2s","5c5d","5c5s","5c4h","5c4c","5c4d","5c4s","5c3h","5c3c","5c3d","5c3s","5c2h","5c2c","5c2d","5c2s","5d5s","5d4h","5d4c","5d4d","5d4s","5d3h","5d3c","5d3d","5d3s","5d2h","5d2c","5d2d","5d2s","5s4h","5s4c","5s4d","5s4s","5s3h","5s3c","5s3d","5s3s","5s2h","5s2c","5s2d","5s2s","4h4c","4h4d","4h4s","4h3h","4h3c","4h3d","4h3s","4h2h","4h2c","4h2d","4h2s","4c4d","4c4s","4c3h","4c3c","4c3d","4c3s","4c2h","4c2c","4c2d","4c2s","4d4s","4d3h","4d3c","4d3d","4d3s","4d2h","4d2c","4d2d","4d2s","4s3h","4s3c","4s3d","4s3s","4s2h","4s2c","4s2d","4s2s","3h3c","3h3d","3h3s","3h2h","3h2c","3h2d","3h2s","3c3d","3c3s","3c2h","3c2c","3c2d","3c2s","3d3s","3d2h","3d2c","3d2d","3d2s","3s2h","3s2c","3s2d","3s2s","2h2c","2h2d","2h2s","2c2d","2c2s","2d2s");


$array1_sorted = usort( $array1, 'cmp' );

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

为什么var_dump($ array1_sorted)结果为“TRUE”?

提前致谢

1 个答案:

答案 0 :(得分:1)

usort返回bool,您的排序数组位于$array1