Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Sort php array by particular column

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 257
    Comment on it

    Hello friends,

    Most of time we need to sort php array as ascending order or descending order but some case we need to sort array by particular column.

    For Example:

    Given Array:

    $inventory = array(
    
       array("type"=>"fruit", "price"=>3.50),
       array("type"=>"milk", "price"=>2.90),
       array("type"=>"pork", "price"=>5.43),
    
    );

    Logic of sort array

    $price = array();
    foreach ($inventory as $key => $row)
    {
        $price[$key] = $row['price'];
    }
    array_multisort($price, SORT_DESC, $inventory);

    Output Array:

    Array
    (
        [0] => Array
            (
                [type] => pork
                [price] => 5.43
            )
    
        [1] => Array
            (
                [type] => fruit
                [price] => 3.5
            )
    
        [2] => Array
            (
                [type] => milk
                [price] => 2.9
            )
    
    )

 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: