1. 程式人生 > >PHP中sort排序的的使用方式及表現形式

PHP中sort排序的的使用方式及表現形式

ArrayUtils::iteratorToArray()
//zend框架中用於列印物件
//關於逆向的一些問題
$fruits = array("lemon", "orange", "banana", "apple");
$fruits = array('o1','sam8','look9','false5','tu7','laske6','doum2','ak3','ye4');
//[0]=>  "ye4" [1]=>"tu7" [2]=> "sam8" [3]=>  "o1" [4]=> "look9" [5]=>  "laske6" [6]=>  "false5" [7]=> "doum2" [8]=>"ak3"
//$fruits = array(false,true,false,true);
// { [0]=> bool(true) [1]=> bool(true) [2]=> bool(false) [3]=> bool(false)
$fruits = array('ye1','ye8','ye9','ye5','ye7','ye6','ye2','ye3','ye4');
// [0]=> "ye9" [1]=>  "ye8" [2]=>  "ye7" [3]=>  "ye6" [4]=>  "ye5" [5]=>  "ye4" [6]=>  "ye3" [7]=> "ye2" [8]=>  "ye1"
$fruits = array(1,'ye8','ye9','ye5',7,'ye6',2,'ye3','ye4');
//[0]=> int(7) [1]=> int(2) [2]=> int(1) [3]=> string(3) "ye9" [4]=>  "ye8" [5]=>  "ye6" [6]=> "ye5" [7]=>  "ye4" [8]=> "ye3"
rsort($fruits);//倒序鍵名
sort($fruits);//正序鍵名
$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");

arsort($fruits);//正序,陣列的索引保持和單元的關聯 根據鍵名正序
//["a"]=> string(6) "orange" ["d"]=> string(5) "lemon" ["b"]=> string(6) "banana" ["c"]=> string(5) "apple"

asort($fruits);//正序,陣列的索引保持和單元的關聯 根據鍵值正序
//["c"]=> string(5) "apple" ["b"]=> string(6) "banana" ["d"]=> string(5) "lemon" ["a"]=> string(6) "orange"

ksort($fruits);//對陣列按照鍵名排序
//["a"]=> string(6) "orange" ["b"]=> string(6) "banana" ["c"]=> string(5) "apple" ["d"]=> string(5) "lemon"

$fruits = array(1,'ye8','ye9','ye5',7,'ye6',2,'ye3','ye4');
natsort($fruits);//自然排序法-針對數字型索引
//[0]=> int(1) [6]=> int(2) [4]=> int(7) [7]=> string(3) "ye3" [8]=> string(3) "ye4" [3]=> string(3) "ye5" [5]=> string(3) "ye6" [1]=> string(3) "ye8" [2]=> string(3) "ye9"

//foreach ($fruits as $key => $val) {
//    echo "$key = $val ";
//}
function cmp($a, $b)
{
    if ($a == $b) {
        return 0;
    }
    return ($a > $b) ? 1 : -1;
    //return ($a > $b) ? false : true;
}
$a = array(4 => "four", 3 => "three", 20 => "twenty", 10 => "ten");
uksort($a, "cmp");//使用使用者自定義的比較函式對陣列中的鍵名進行排序
//return 回去的就是 排序的方式