0

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?

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, I've had a quick look but I#m not entirely sure what is passed to the function cmp. What is $a and what is $b?
$a and $b are two items from the array that need to be compared. Because you have a multidimensional array you need to access your comparable values like so $a[0] and $b[0]. Although this could be wrong as your array is hard to follow in its current format.
I don't see how using this will allow me to sort the array according to ascending alphabetical order.
The comparison function is provided with two elements from the array, and should return whether the first element should be placed before or after the second element in the final order.
1

Look at using usort() http://php.net/manual/en/function.usort.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.