PHP Sort Associative Array Key Length

Howdy all,
  Does anyone have a good fast method for sorting an associative array by the key's string length?  Example:

$m['aa'] = true;
$m['abb'] = true;
$m['b'] = true;
$m['dddd'] = true;
$m['bb'] = true;
$m['c'] = true;

Will get sorted as:

$m['dddd']
$m['abb']
$m['aa']
$m['bb']
$m['b']
$m['c']

See?  Sorting by the longest index/key to the shortest.  Also it's likely that the arrays will be multidimensional with associative arrays inside, so I need to maintaining key to data correlations all the way through.
headzooAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

headzooAuthor Commented:
Just a quick clarification: Even though the associative array is multidimensional, the arrays "inside" shouldn't affect the sorting at all.  Example:

$m['aa'] = array('bb' => true, 'aa' => true);
$m['bbb'] = array('a' => true, 'b' => true);

Will get sorted as:

$m['bbb'] = array('a' => true, 'b' => true);
$m['aa'] = array('bb' => true, 'aa' => true);

Only the base key's need to be sorted.
RoonaanCommented:
You can use uksort to define your own sorting function:

<?php

$m['aa'] =  array('bb' => true, 'aa' => true);
$m['abb'] =  array('bb' => true, 'aa' => true);
$m['b'] =  array('bb' => true, 'aa' => true);
$m['dddd'] =  array('bb' => true, 'aa' => true);
$m['bb'] =  array('bb' => true, 'aa' => true);
$m['c'] =  array('bb' => true, 'aa' => true);

uksort($m, 'mySort');

print_r($m);

function mySort($key1, $key2) {
  $s1 = strlen($key1);
  $s2 = strlen($key2);
  if($s1 == $s2) return 0;
 
  return $s1 > $s2 ? -1 : 1;
}

?>

-r-

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
headzooAuthor Commented:
Brilliant!  Thank you.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.