简体   繁体   中英

CakePHP 2.4.4 How can I sort a multi-dimensional array with Hash::sort by string keys and values?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM