Search the blog

This handy function sorts associative arrays by a given child key but also allows you to keep numerical indices. By default, array_multisort() — which is useful for this kind of thing — removes the keys.

<?php 

function sortAssocArrayByValue($arrayToSort, $sortKey, $isAsc = true, $keepKeys = false) {

    if ($isAsc === true) {

        $sort = SORT_ASC;

    } else {

        $sort = SORT_DESC;

    }

    $array 	= [];
    $data	= [];

    // The keys are preserved by making them strings
    foreach ($arrayToSort as $key => $value) {

        if ($keepKeys === true) {

           $k = '_' . $key;

        } else {

            $k = $key;   

        }

        $data[$k]	= $value;
        $array[$k] 	= $value[$sortKey];

    }

    // This sorts the data based on $array
    array_multisort($array, $sort, $data);

    // If the keys are not being kept then the work is done
    if ($keepKeys === false) {

       return $data; 

    }

    // To keep the keys the new array overwrites the old one and the numerical keys are restored
    $arrayToSort = [];

    foreach ($data as $key => $value) {

        $arrayToSort[ltrim($key, '_')] = $value;

    }

    return $arrayToSort;

}

// A test numerical array
$array1 = [

    ['name' => 'TV', 'price' => 499.99],
    ['name' => 'Power cable', 'price' => 9.99],
    ['name' => 'Laptop', 'price' => 990.00]

];

// A test associative array
$array2 = [

    'tv' => ['name' => 'TV', 'price' => 499.99],
    'cable' => ['name' => 'Power cable', 'price' => 9.99],
    'laptop' => ['name' => 'Laptop', 'price' => 990.00]

];

?>
<pre>
Numerical array, keys kept:
<?php print_r(sortAssocArrayByValue($array1, 'name',  true,  true)) ?>
<?php print_r(sortAssocArrayByValue($array1, 'name',  false, true)) ?>
<?php print_r(sortAssocArrayByValue($array1, 'price', true,  true)) ?>
<?php print_r(sortAssocArrayByValue($array1, 'price', false, true)) ?>
Numerical array, keys reset:
<?php print_r(sortAssocArrayByValue($array1, 'name',  true,  false)) ?>
<?php print_r(sortAssocArrayByValue($array1, 'name',  false, false)) ?>
<?php print_r(sortAssocArrayByValue($array1, 'price', true,  false)) ?>
<?php print_r(sortAssocArrayByValue($array1, 'price', false, false)) ?>
Associative array:
<?php print_r(sortAssocArrayByValue($array2, 'name',  true)) ?>
<?php print_r(sortAssocArrayByValue($array2, 'name',  false)) ?>
<?php print_r(sortAssocArrayByValue($array2, 'price', true)) ?>
<?php print_r(sortAssocArrayByValue($array2, 'price', false)) ?>

</pre>
<?php 

/*

Output:

Numerical array, keys kept:
Array
(
    [2] => Array
        (
            [name] => Laptop
            [price] => 990.00
        )

    [1] => Array
        (
            [name] => Power cable
            [price] => 9.99
        )

    [0] => Array
        (
            [name] => TV
            [price] => 499.99
        )

)
Array
(
    [0] => Array
        (
            [name] => TV
            [price] => 499.99
        )

    [1] => Array
        (
            [name] => Power cable
            [price] => 9.99
        )

    [2] => Array
        (
            [name] => Laptop
            [price] => 990.00
        )

)
Array
(
    [1] => Array
        (
            [name] => Power cable
            [price] => 9.99
        )

    [0] => Array
        (
            [name] => TV
            [price] => 499.99
        )

    [2] => Array
        (
            [name] => Laptop
            [price] => 990.00
        )

)
Array
(
    [2] => Array
        (
            [name] => Laptop
            [price] => 990.00
        )

    [0] => Array
        (
            [name] => TV
            [price] => 499.99
        )

    [1] => Array
        (
            [name] => Power cable
            [price] => 9.99
        )

)
Numerical array, keys reset:
Array
(
    [0] => Array
        (
            [name] => Laptop
            [price] => 990.00
        )

    [1] => Array
        (
            [name] => Power cable
            [price] => 9.99
        )

    [2] => Array
        (
            [name] => TV
            [price] => 499.99
        )

)
Array
(
    [0] => Array
        (
            [name] => TV
            [price] => 499.99
        )

    [1] => Array
        (
            [name] => Power cable
            [price] => 9.99
        )

    [2] => Array
        (
            [name] => Laptop
            [price] => 990.00
        )

)
Array
(
    [0] => Array
        (
            [name] => Power cable
            [price] => 9.99
        )

    [1] => Array
        (
            [name] => TV
            [price] => 499.99
        )

    [2] => Array
        (
            [name] => Laptop
            [price] => 990.00
        )

)
Array
(
    [0] => Array
        (
            [name] => Laptop
            [price] => 990.00
        )

    [1] => Array
        (
            [name] => TV
            [price] => 499.99
        )

    [2] => Array
        (
            [name] => Power cable
            [price] => 9.99
        )

)
Associative array:
Array
(
    [laptop] => Array
        (
            [name] => Laptop
            [price] => 990.00
        )

    [cable] => Array
        (
            [name] => Power cable
            [price] => 9.99
        )

    [tv] => Array
        (
            [name] => TV
            [price] => 499.99
        )

)
Array
(
    [tv] => Array
        (
            [name] => TV
            [price] => 499.99
        )

    [cable] => Array
        (
            [name] => Power cable
            [price] => 9.99
        )

    [laptop] => Array
        (
            [name] => Laptop
            [price] => 990.00
        )

)
Array
(
    [cable] => Array
        (
            [name] => Power cable
            [price] => 9.99
        )

    [tv] => Array
        (
            [name] => TV
            [price] => 499.99
        )

    [laptop] => Array
        (
            [name] => Laptop
            [price] => 990.00
        )

)
Array
(
    [laptop] => Array
        (
            [name] => Laptop
            [price] => 990.00
        )

    [tv] => Array
        (
            [name] => TV
            [price] => 499.99
        )

    [cable] => Array
        (
            [name] => Power cable
            [price] => 9.99
        )

)

*/

?>
Tim Bennett is a freelance web designer from Leeds. He has a First Class Honours degree in Computing from Leeds Metropolitan University and currently runs his own one-man web design company, Texelate.