PHP define constants with arrays

You need to define a constant containing an array but the code below doesn’t work ?

define('FILTER_ECOLE', array('price, categorie, niveau'));

You get an error :

Notice: Use of undefined constant FILTER_ECOLE - assumed 'FILTER_ECOLE'  in /home/www/adin/app/code/local/Adin/Catalog/Block/Nav/Catalog/Layer/View/Sidebar.php on line 50

To define an use a constant containing an array you can simply serialize it :

#define
define('FILTER_ECOLE', serialize(array('price, categorie, niveau')));

#use
$filter = unserialize(FILTER_ECOLE);

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;
  }