Sorting a multidimensional array in alphabetical order

How can I sort an array like this in alphabetical order:

$allowed = array(
  'pre'    => array(),
  'code'   => array(),
  'a'      => array(
                'href'  => array(),
                'title' => array()
              ),
  'strong' => array(),
  'em'     => array(),
);

// sort($allowed); ?

?

+5
source share
4 answers

Yeah! You needuksort();

Comparing PHP Sort Functions (helpful lady)

Edit: The reason is that you seem to want to sort inside arrays? AFAIK ksort by itself does not do this - it directly ignores the value of the original array.

Edit2: This should work (although it uses recursion instead of kusort):

function ksort_deep(&$array){
    ksort($array);
    foreach($array as &$value)
        if(is_array($value))
            ksort_deep($value);
}

// example of use:
ksort_deep($allowed);

// see it in action
echo '<pre>'.print_r($allowed,true).'</pre>';

: uksort(), , . , :)

+9
+4

You're using

ksort($allowed);

http://php.net/manual/en/function.ksort.php

+2
source
bool ksort ( array &$array [, int $sort_flags = SORT_REGULAR ] )

as described here . The See Also section is usually very useful.

+2
source

All Articles