0

I don't understand the docs regarding the Hash 'path', so I've had no luck. I'm trying to sort each layer alphabetically:

array(
'music' => array(
    'genre' => array(
        (int) 0 => 'Dubstep',
        (int) 1 => 'Blues',
        (int) 2 => 'Classical'
    ),
    'instrument' => array(
        (int) 0 => 'Guitar (Electric)',
        (int) 1 => 'Bassoon',
        (int) 2 => 'Harmonica (Diatonic)'
    ),
'anotherLot' => array(

I need to sort the first later of arrays by key, then the second later in each by key, and the third by the values, so I imagine the two deeper layers would be done with a nested foreach.

1 Answer 1

1

I'm not familiar with CakePHP's Hash class, but here is a plain PHP solution:

ksort($data); // sort main array by keys

foreach ($data as &$outer)
{
    ksort($outer); // sort next layer by keys
    foreach($outer as &$inner) 
    {
        asort($inner); // sort inner arrays by values
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I'm unsure if Hash::sort can do any better. I used natcasesort() instead of asort() for natural, case-insensitive ordering.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.