William Jiang

JavaScript,PHP,Node,Perl,LAMP Web Developer – http://williamjxj.com; https://github.com/williamjxj?tab=repositories

PHP Array Sort Summary with examples

PHP Array Sort Summary with examples by William.

  1. bool sort ( array &$array [, int $sort_flags= SORT_REGULAR ] ): Sort an array
        <?php
        //
        $fruits = array("lemon", "orange", "banana", "apple");
        sort($fruits);
        print_r($fruits);
        ?>
        Array
        (
        [0] => apple
        [1] => banana
        [2] => lemon
        [3] => orange
        )
  2. bool rsort ( array &$array [, int $sort_flags = SORT_REGULAR ] ): Sort an array in reverse order
    <?php
        $fruits = array("lemon", "orange", "banana", "apple");
        rsort($fruits);
        print_r($fruits);
        ?>
        Array
        (
        [0] => orange
        [1] => lemon
        [2] => banana
        [3] => apple
        ) 
        
  3. bool asort ( array &$array [, int $sort_flags = SORT_REGULAR ] ): Sort an array and maintain index association
    <?php
        $fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
        asort($fruits);
        print_r($fruits);
        ?>
        Array
        (
        [c] => apple
        [b] => banana
        [d] => lemon
        [a] => orange
        ) 
  4. bool arsort ( array &$array [, int $sort_flags = SORT_REGULAR ] ):Sort an array in reverse order and maintain index association
    <?php
        $fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
        arsort($fruits);
        print_r($fruits);
        ?>
        Array
        (
        [a] => orange
        [d] => lemon
        [b] => banana
        [c] => apple
        ) 
  5. bool ksort ( array &$array [, int $sort_flags = SORT_REGULAR ] ):Sort an array by key
    <?php
        $fruits = array("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");
        ksort($fruits);
        print_r($fruits);
        ?>
        Array
        (
        [a] => orange
        [b] => banana
        [c] => apple
        [d] => lemon
        ) 
  6. bool krsort ( array &$array [, int $sort_flags = SORT_REGULAR ] ):Sort an array by key in reverse order
    <?php
        $fruits = array("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");
        krsort($fruits);
        print_r($fruits);
        ?>
        Array
        (
        [d] => lemon
        [c] => apple
        [b] => banana
        [a] => orange
        ) 
  7. bool usort ( array &$array , callback $cmp_function ):Sort an array by values using a user-defined comparison function
    <?php
        function cmp($a, $b) {
        if ($a == $b) {
        return 0;
        }
        return ($a < $b) ? -1 : 1;
        }
        $a = array(3, 2, 5, 6, 1);
        usort($a, "cmp");
        print_r($a);
        ?>
        Array
        (
        [0] => 1
        [1] => 2
        [2] => 3
        [3] => 5
        [4] => 6
        ) 
  8. bool uksort ( array &$array , callback $cmp_function ):Sort an array by keys using a user-defined comparison function
    <?php
        function cmp1($a, $b){
        $a = preg_replace('@^(a|an|the) @', '', $a);
        $b = preg_replace('@^(a|an|the) @', '', $b);
        return strcasecmp($a, $b);
        }
        $a = array("John" => 1, "the Earth" => 2, "an apple" => 3, "a banana" => 4);
        uksort($a, "cmp1");
        print_r($a);
        ?>
        Array
        (
        [an apple] => 3
        [a banana] => 4
        [the Earth] => 2
        [John] => 1
        )     
  9. bool uasort ( array &$array , callback $cmp_function ):Sort an array with a user-defined comparison function and maintain index association
    <?php
        // Comparison function
        function cmp2($a, $b) {
        if ($a == $b) {
        return 0;
        }
        return ($a < $b) ? -1 : 1;
        }
        // Array to be sorted
        $array = array('a' => 4, 'b' => 8, 'c' => -1, 'd' => -9, 'e' => 2, 'f' => 5, 'g' => 3, 'h' => -4);
        // Sort and print the resulting array
        uasort($array, 'cmp2');
        print_r($array);
        ?> 
        Array
        (
        [d] => -9
        [h] => -4
        [c] => -1
        [e] => 2
        [g] => 3
        [a] => 4
        [f] => 5
        [b] => 8
        ) 
  10. bool array_multisort ( array &$arr [, mixed $arg = SORT_ASC [, mixed $arg = SORT_REGULAR [, mixed $… ]]] ):Sort multiple or multi-dimensional arrays
    <?php
        $ar = array(
        array("10", 11, 100, 100, "a"),
        array(   1,  2, "2",   3,   1)
        );
        
        array_multisort($ar[0], SORT_ASC, SORT_STRING,
        $ar[1], SORT_NUMERIC, SORT_DESC);
        var_dump($ar);
        ?>
        array
        0 => 
        array
        0 => string '10' (length=2)
        1 => int 100
        2 => int 100
        3 => int 11
        4 => string 'a' (length=1)
        1 => 
        array
        0 => int 1
        1 => int 3
        2 => string '2' (length=1)
        3 => int 2
        4 => int 1 
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: