Search code examples
phpsortingarrayobject

Sorting an array


If I have an array of data, what is the best option for sorting them so they are displayed in ascending alphabetical order based on key 2 of the second array within each ArrayObject?

Data

ArrayObject::__set_state(array(
   'job_category_filter_population' => 
  ArrayObject::__set_state(array(
     10225 => 
    ArrayObject::__set_state(array(
       0 => 
      array (
        0 => '10042',
        1 => 'Root',
      ),
       1 => 
      array (
        0 => '10225',
        1 => 'Supply',
      ),
    )),
     10228 => 
    ArrayObject::__set_state(array(
       0 => 
      array (
        0 => '10042',
        1 => 'Root',
      ),
       1 => 
      array (
        0 => '10228',
        1 => 'X-ray',
      ),
    )),
     10226 => 
    ArrayObject::__set_state(array(
       0 => 
      array (
        0 => '10042',
        1 => 'Root',
      ),
       1 => 
      array (
        0 => '10226',
        1 => 'Team',
      ),
    ))
  ))
))

E.g. Supply, Team and then X-ray?


Solution

  • One would use ArrayObject::uasort and provide a callback function that compares the second element of the second array of its arguments.