Quick PHP Function #3: Sorting multi-dimensional arrays by a value within

Another useful function im frequently in need of, a multidimensional array sort/order function

function array_sort_by_column(&$arr, $col, $dir = SORT_ASC) {
    $sort_col = array();
    foreach ($arr as $key=> $row) {
        $sort_col[$key] = $row[$col];

    array_multisort($sort_col, $dir, $arr);

Use it the same way you would use asort($arrayname) but with a second argument being the name of the column/key name you wish to sort by

array_sort_by_column($arrayname ,"keyname");

$arrayname will now be sorted to the value of the given keyname


Leave a Reply

Your email address will not be published. Required fields are marked *