Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to sort an given array by it's any key index

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 171
    Comment on it

    Hello Reader's! if you have an given array on with you want to sort with any selected key index as show below:-

    Array
            (
                [0] => Array
                    (
                        [configuration_id] => 10
                        [id] => 1
                        [optionNumber] => 3
                        [optionActive] => 1
                        [lastUpdated] => 2010-03-17 15:44:12
                    )
    
                [1] => Array
                    (
                        [configuration_id] => 9
                        [id] => 1
                        [optionNumber] => 2
                        [optionActive] => 1
                        [lastUpdated] => 2010-03-17 15:44:12
                    )
    
                [2] => Array
                    (
                        [configuration_id] => 8
                        [id] => 1
                        [optionNumber] => 1
                        [optionActive] => 1
                        [lastUpdated] => 2010-03-17 15:44:12
                    )
        )
    

    And you want your array to be like this:-

     Array
                (
                    [0] => Array
                        (
                            [configuration_id] => 8
                            [id] => 1
                            [optionNumber] => 1
                            [optionActive] => 1
                            [lastUpdated] => 2010-03-17 15:44:12
                        )
    
                    [1] => Array
                        (
                            [configuration_id] => 9
                            [id] => 1
                            [optionNumber] => 2
                            [optionActive] => 1
                            [lastUpdated] => 2010-03-17 15:44:12
                        )
    
                    [2] => Array
                        (
                            [configuration_id] => 10
                            [id] => 1
                            [optionNumber] => 3
                            [optionActive] => 1
                            [lastUpdated] => 2010-03-17 15:44:12
                        )
            )
    

    Now you have to use the code as written below:-

    function cmp_by_optionNumber($a, $b) {
      return $a["optionNumber"] - $b["optionNumber"];
    }
    
    usort($array, "cmp_by_optionNumber");
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: