SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Guru ripcurlksm's Avatar
    Join Date
    Aug 2004
    Location
    San Clemente, CA
    Posts
    859
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    sort associative array with capital letters

    Using ksort() worked great until some names had capital letters.

    PHP Code:
    <?php
    $my_array 
    = Array();
    $my_array = array(
        
    'Advent'    => 'A short description',
        
    'AZip'        => 'A short description',
        
    'Birch'        => 'A short description'
    );

    ksort($my_array);

    echo 
    "<ol>";

    foreach(
    $my_array as $name => $description){
        echo 
    "<li>$name - $description</li>";
    }

    echo 
    "</ol>";

    // Outputs AZip as #1 instead of #2
    //   1. AZip - A short description
    //   2. Advent - A short description
    //   3. Birch - A short description
    ?>
    What should I be using to alphabetize an associative array that ignores uppercase letters?

  2. #2
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    61 Post(s)
    Tagged
    0 Thread(s)
    You can use uksort in conjunction with a function that will compare strings (i.e., keys in this case) case-insensitively like strnatcasecmp. To give a practical example:

    PHP Code:
    <?php
    $my_array 
    = array(
        
    'Birch' => 'A short description',
        
    'Advent'=> 'A short description',
        
    'AZip'  => 'A short description'
    );
    uksort($my_array'strnatcasecmp');
    print_r($my_array);
    ?>
    Outputs something like the following (note the key order):
    Code:
    Array
    (
        [Advent] => A short description
        [AZip] => A short description
        [Birch] => A short description
    )
    Last edited by Salathe; Nov 4, 2009 at 17:35. Reason: Changed example to show it actually doing something.
    Salathe
    Software Developer and PHP Manual Author.

  3. #3
    SitePoint Guru ripcurlksm's Avatar
    Join Date
    Aug 2004
    Location
    San Clemente, CA
    Posts
    859
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That worked great, thanks again Sal!

  4. #4
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    61 Post(s)
    Tagged
    0 Thread(s)
    No problem, happy to help out.
    Salathe
    Software Developer and PHP Manual Author.

  5. #5
    SitePoint Enthusiast
    Join Date
    Jul 2009
    Location
    nowhere
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can use natcasesort function either. I searched this solution in our manual.

    PHP Code:
    $my_array = array(
        
    'Advent'    => 'A short description',
        
    'AcXip'        => 'A short description',
        
    'AZip'        => 'A short description',
        
    'AXip'        => 'A short description',
        
    'Birch'        => 'A short description'
    );

    function 
    natcasesort_($my_array) {

      
    $keys array_keys($my_array);
      
    natcasesort($keys);

      foreach (
    $keys as $k)
        
    $new_array[$k] = $my_array[$k];

      return 
    $new_array;
    }

    $a natcasesort_($my_array);
    var_dump($a); 
    Last edited by 3.grosz; Nov 4, 2009 at 18:17. Reason: corr

  6. #6
    SitePoint Guru ripcurlksm's Avatar
    Join Date
    Aug 2004
    Location
    San Clemente, CA
    Posts
    859
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    tyvm!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •