Sorting an associative array alphabetically I am using a foreach loop to sort an associative array alphabetically. I would like to know if there is a more proper and/or efficient way of doing it.

The array:

Array
(
    [gr_c] =&gt; Array
        (
            [f] =&gt; 'value...'
            [a] =&gt; 'value...'
            [d] =&gt; 'value...'
            [m] =&gt; 'value...'
            [c] =&gt; 'value...'
            [t] =&gt; 'value...'
        )

    [gr_a] =&gt; Array
        (
            [h] =&gt; 'value...'
            [e] =&gt; 'value...'
            [m] =&gt; 'value...'
            [a] =&gt; 'value...'
            [o] =&gt; 'value...'
            [i] =&gt; 'value...'
            [c] =&gt; 'value...'
            [t] =&gt; 'value...'
            [b] =&gt; 'value...'
        )

    [gr_b] =&gt; Array
        (
            [h] =&gt; 'value...'
            [d] =&gt; 'value...'
        )
)


became:    

Array
(
    [gr_c] =&gt; Array
        (
            [a] =&gt; 'value...'
            [c] =&gt; 'value...'
            [d] =&gt; 'value...'
            [f] =&gt; 'value...'
            [m] =&gt; 'value...'
            [t] =&gt; 'value...'
        )

    [gr_a] =&gt; Array
        (
            [a] =&gt; 'value...'
            [b] =&gt; 'value...'
            [c] =&gt; 'value...'
            [e] =&gt; 'value...'
            [h] =&gt; 'value...'
            [i] =&gt; 'value...'
            [m] =&gt; 'value...'
            [o] =&gt; 'value...'
            [t] =&gt; 'value...'
        )

    [gr_b] =&gt; Array
        (
            [d] =&gt; 'value...'
            [h] =&gt; 'value...'
        )
)


used snippet:

foreach ($array_name as $key =&gt; $value) {
    ksort($array_name[$key]);
}
That snippet of 3 lines you used,
is fine as it is, nothing really wrong with it.
It's proper, efficient, natural, easy to understand.

There is just one thing I'd pick on,
is that the $value variable in the foreach expression is not used.
Another way to achieve the same thing without unused variables is to use &amp; to pass the loop variable by reference:

foreach ($array_name as &amp;$arr) {
    ksort($arr);
}


This has the advantage that the loop index variable $key is now gone too,
we're working with the data that really matters, which is the $arr to sort.