PHP Sort Multi-Dimensional Array by Key

Hi Experts,

How can I sort the array below by ['client_count'] Descending, so that highest client numbers are on top?

Array
(
    [3] => Array
        (
            [id] => 2
            [name] => Computers (Refurbished)
            [clients] => Array
                (
                    [0] => 29
                )

            [client_count] => 1
        )

    [4] => Array
        (
            [id] => 1
            [name] => Continued Education
            [clients] => Array
                (
                    [0] => 29
                )

            [client_count] => 1
        )

    [6] => Array
        (
            [id] => 12
            [name] => Debt Management
            [clients] => Array
                (
                    [0] => 1
                )

            [client_count] => 1
        )

    [9] => Array
        (
            [id] => 10
            [name] => Employment / Self Employment
            [clients] => Array
                (
                    [0] => 1
                )

            [client_count] => 1
        )

    [10] => Array
        (
            [id] => 11
            [name] => Family Matters
            [clients] => Array
                (
                    [0] => 3
                )

            [client_count] => 1
        )

    [11] => Array
        (
            [id] => 13
            [name] => Housing / Settlement
            [clients] => Array
                (
                    [0] => 2
                )

            [client_count] => 1
        )

    [13] => Array
        (
            [id] => 15
            [name] => New Immigrant Service
            [clients] => Array
                (
                    [0] => 37
                )

            [client_count] => 1
        )

    [14] => Array
        (
            [id] => 23
            [name] => Other (please specify)
            [clients] => Array
                (
                    [0] => 37
                )

            [client_count] => 1
        )

    [15] => Array
        (
            [id] => 16
            [name] => Senior Support
            [clients] => Array
                (
                    [0] => 3
                    [1] => 37
                )

            [client_count] => 2
        )

    [20] => Array
        (
            [id] => 19
            [name] => Translation
            [clients] => Array
                (
                    [0] => 37
                )

            [client_count] => 1
        )

)

Open in new window

APD TorontoAsked:
Who is Participating?
 
Julian HansenCommented:
Take a look at uasort()
Code you need
uasort($x, function($a, $b) {
	return $b['client_count'] - $a['client_count']; 
});

Open in new window

Working sample
<?php
$x = Array
(
    "3" => Array
        (
            "id" => 2,
            "name" => "Computers (Refurbished)",
            "clients" => Array
                (
                    "0" => 29
                ),

            "client_count" => 1
        ),

    "4" => Array
        (
            "id" => 1,
            "name" => "Continued Education",
            "clients" => Array
                (
                    "0" => 29
                ),

            "client_count" => 1
        ),

    "6" => Array
        (
            "id" => 12,
            "name" => "Debt Management",
            "clients" => Array
                (
                    "0" => 1
                ),

            "client_count" => 1
        ),

    "9" => Array
        (
            "id" => 10,
            "name" => "Employment / Self Employment",
            "clients" => Array
                (
                    "0" => 1
                ),

            "client_count" => 1
        ),

    "10" => Array
        (
            "id" => 11,
            "name" => "Family Matters",
            "clients" => Array
                (
                    "0" => 3
                ),

            "client_count" => 1
        ),

    "11" => Array
        (
            "id" => 13,
            "name" => "Housing / Settlement",
            "clients" => Array
                (
                    "0" => 2
                ),

            "client_count" => 1
        ),

    "13" => Array
        (
            "id" => 15,
            "name" => "New Immigrant Service",
            "clients" => Array
                (
                    "0" => 37
                ),

            "client_count" => 1
        ),

    "14" => Array
        (
            "id" => 23,
            "name" => "Other (please specify)",
            "clients" => Array
                (
                    "0" => 37
                ),

            "client_count" => 1
        ),

    "15" => Array
        (
            "id" => 16,
            "name" => "Senior Support",
            "clients" => Array
                (
                    "0" => 3,
                    "1" => 37
                ),

            "client_count" => 2
        ),

    "20" => Array
        (
            "id" => 19,
            "name" => "Translation",
            "clients" => Array
                (
                    "0" => 37
                ),

            "client_count" => 1
        )

);
uasort($x, function($a, $b) {
	return $b['client_count'] - $a['client_count']; 
});
echo "<pre>";
print_r($x);
echo "</pre>";

Open in new window

0
 
APD TorontoAuthor Commented:
...But, in your example, you'll see Li. 110 client count = 2, which should bring Senior Support on top
0
 
APD TorontoAuthor Commented:
Nevermind, got it !:)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.