PHP Sort Associative Array Key Length

Posted on 2006-03-24
Last Modified: 2012-05-05
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:


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.
Question by:headzoo

    Author Comment

    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.
    LVL 49

    Accepted Solution

    You can use uksort to define your own sorting function:


    $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');


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



    Author Comment

    Brilliant!  Thank you.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
    Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now