Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting an array by a certain key

I have the following array:

Array
(
    [Places] => Array
        (
            [public] => 0
            [entities] => Array
                (
                    ...
                )
        )
    [Issues] => Array
        (
            [public] => 1
            [entities] => Array
                (
                    ...
                )
        )

    [Source] => Array
        (
            [public] => 0
            [entities] => Array
                (
                    ...
                )
        )
)

I would like to be able to sort by the array by the public key. I know I may have to use either ksort or usort but I am unsure on how to implement this.

Any ideas would be great thanks!

like image 629
Lizard Avatar asked Aug 22 '26 21:08

Lizard


2 Answers

usort($array, function ($a, $b) { return $a["public"] - $b["public"]; });
like image 64
Artefacto Avatar answered Aug 25 '26 12:08

Artefacto


Here's an interesting link: http://www.the-art-of-web.com/php/sortarray/

I would try a

usort(usort(array, function), function);

I can try a sample code upon request, but the information is already there for the taking.

like image 35
Justian Meyer Avatar answered Aug 25 '26 12:08

Justian Meyer