PHP sort array by sub value

Here my array structure :

Array
(
    [33] => Array
        (
            [id_site] => 33
            [datas] => Array
                (
                    [id] => 2965
                    [site_id] => 33
                    [operation_id] => 20
                    [positionnement] => 1
                )
            [statut] => Validé DM
            [sort] => 0119
        )
    [32] => Array
        (
            [id_site] => 32
            [datas] => Array
                (
                    [id] => 1929
                    [site_id] => 32
                    [operation_id] => 20
                    [positionnement] => 1
                 )
            [statut] => Validé DM
            [sort] => 0114
        )
    [34] => Array
        (
            [id_site] => 34
            [datas] => Array
                (
                    [id] => 2230
                    [site_id] => 34
                    [operation_id] => 20
                    [positionnement] => 1
                )
            [statut] => Validé DM
            [sort] => 0128
        )
)

How to sort by the [sort] value inside my array ?

I have to use the usort() function :

function sortByCustom($a, $b) {
    return strcmp($a['sort'], $b['sort']);
}
...
usort($array, 'sortByCustom');

Here the result :

Array
(
    [0] => Array
        (
            [id_site] => 32
            [datas] => Array
                (
                    [id] => 1929
                    [site_id] => 32
                    [operation_id] => 20
                    [positionnement] => 1
                 )
            [statut] => Validé DM
            [sort] => 0114
        )

    [1] => Array
        (
            [id_site] => 33
            [datas] => Array
                (
                    [id] => 2965
                    [site_id] => 33
                    [operation_id] => 20
                    [positionnement] => 1
                )
            [statut] => Validé DM
            [sort] => 0119
        )
    [2] => Array
        (
            [id_site] => 34
            [datas] => Array
                (
                    [id] => 2230
                    [site_id] => 34
                    [operation_id] => 20
                    [positionnement] => 1
                )
            [statut] => Validé DM
            [sort] => 0128
        )
)

If you want to maintain the index value association, use the uasort() function

uasort($array, 'sortByCustom');

Here the result :

Array
(
    [32] => Array
        (
            [id_site] => 32
            [datas] => Array
                (
                    [id] => 1929
                    [site_id] => 32
                    [operation_id] => 20
                    [positionnement] => 1
                 )
            [statut] => Validé DM
            [sort] => 0114
        )

    [33] => Array
        (
            [id_site] => 33
            [datas] => Array
                (
                    [id] => 2965
                    [site_id] => 33
                    [operation_id] => 20
                    [positionnement] => 1
                )
            [statut] => Validé DM
            [sort] => 0119
        )

    [34] => Array
        (
            [id_site] => 34
            [datas] => Array
                (
                    [id] => 2230
                    [site_id] => 34
                    [operation_id] => 20
                    [positionnement] => 1
                )
            [statut] => Validé DM
            [sort] => 0128
        )
)

If you use this in a class, you have to define the sort function outside the class

<?php
function sortByCustom($a, $b)
{
        return strcmp($a['sort'], $b['sort']);
}
class positionnemen {
...

for strict number comparaison, you can also use this function

  function sortByCustom($a, $b)
  {
      if($a['nbr'] > $b['nbr'])
      {
          return 1;
      }
      return -1;
  }

Leave a Reply