Sorting multidimensional array, upper case being sorted before lowercase

后端 未结 2 561
日久生厌
日久生厌 2021-01-25 02:46

Using the below for sorting a multidimensional array by screen name:

$sortArray = array();

foreach($members as $member){
    foreach($member as $key=>$value)         


        
相关标签:
2条回答
  • 2021-01-25 03:26

    You can sort this with usort() and a custom sorting function. You can modify this data if you like to see how it suits your own custom details: http://codepad.org/b89HwmbM

    $members = array(
      array( "loc" => "Ontario",   "first" => "Bob",  "screen_name" => "fooArt" ),
      array( "loc" => "Atlanta",   "first" => "Sal",  "screen_name" => "TIMMY!" ),
      array( "loc" => "Panama",    "first" => "Mick", "screen_name" => "Analog" ),
      array( "loc" => "Pensacola", "first" => "A",    "screen_name" => "Franky" ),
      array( "loc" => "McPherson", "first" => "Anna", "screen_name" => "annie9" )
    );
    
    usort( $members, 'custom_sort' );
    
    function custom_sort( $m1, $m2 ){
      return strtolower( $m1['screen_name'] ) > strtolower( $m2['screen_name'] );
    };
    

    The output from this sorting method follows:

    Array
    (
        [0] => Array
            (
                [loc] => Panama
                [first] => Mick
                [screen_name] => Analog
            )
    
        [1] => Array
            (
                [loc] => McPherson
                [first] => Anna
                [screen_name] => annie_23
            )
    
        [2] => Array
            (
                [loc] => Ontario
                [first] => Bob
                [screen_name] => fooArt
            )
    
        [3] => Array
            (
                [loc] => Pensacola
                [first] => A
                [screen_name] => Franky
            )
    
        [4] => Array
            (
                [loc] => Atlanta
                [first] => Sal
                [screen_name] => TIMMY!
            )
    
    )
    
    0 讨论(0)
  • 2021-01-25 03:32

    uasort() should work for you

    <?php
    
    // custom sort function
    function sort_by_screen_name($a, $b) {
    
        // compare using lowercase strings only
        $a = strtolower($a['screen_name']);
        $b = strtolower($b['screen_name']);
    
        if ($a == $b) {
            return 0;
        }
    
        return ($a < $b) ? -1 : 1;
    }
    
    $members = array(
        array('screen_name'=>'Greg'),
        array('screen_name'=>'Allen'),
        array('screen_name'=>'dan'),
        array('screen_name'=>'Brenda'),
    );
    
    // uasort() like a boss!
    uasort($members, 'sort_by_screen_name');
    
    print_r($members);
    

    Output

    Array
    (
        [1] => Array
            (
                [screen_name] => Allen
            )
    
        [3] => Array
            (
                [screen_name] => Brenda
            )
    
        [2] => Array
            (
                [screen_name] => dan
            )
    
        [0] => Array
            (
                [screen_name] => Greg
            )
    
    )
    

    Note: the original array keys are preserved using this method. If you'd like the keys to be rewritten, you can simply use usort().

    0 讨论(0)
提交回复
热议问题