SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    Nov 2002
    Location
    Maryland
    Posts
    307
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help with sorting multi-dimensional array

    Hello,
    I have the following array():

    PHP Code:

    $attorneys
    [4]['attorney_first'] = "John";
    $attorneys[4]['attorney_last'] = "Doe";

    $attorneys[5]['attorney_first'] = "Sandy";
    $attorneys[5]['attorney_last'] = "Smith";

    $attorneys[6]['attorney_first'] = "Paul";
    $attorneys[6]['attorney_last'] = "da Costa"
    Now I want to sort this array alphabetically by the last name so I used the following function:

    PHP Code:
    function compare($x$y){
      return 
    strcmp($x['attorney_last'], $y['attorney_last']);


    usort($attorneys'compare'); 
    Everything works fine except for when the last name is in lowercase as in the example above "da Costa". Paul da Costa does not get alphabetized, but the other two names do.

    Anyone know way to solve this problem?

    Thanks

  2. #2
    SitePoint Evangelist
    Join Date
    Apr 2005
    Location
    London, UK
    Posts
    506
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by JeffGSR View Post
    Hello,
    I have the following array():

    PHP Code:

    $attorneys
    [4]['attorney_first'] = "John";
    $attorneys[4]['attorney_last'] = "Doe";

    $attorneys[5]['attorney_first'] = "Sandy";
    $attorneys[5]['attorney_last'] = "Smith";

    $attorneys[6]['attorney_first'] = "Paul";
    $attorneys[6]['attorney_last'] = "da Costa"
    Now I want to sort this array alphabetically by the last name so I used the following function:

    PHP Code:
    function compare($x$y){
      return 
    strcmp($x['attorney_last'], $y['attorney_last']);


    usort($attorneys'compare'); 
    Everything works fine except for when the last name is in lowercase as in the example above "da Costa". Paul da Costa does not get alphabetized, but the other two names do.

    Anyone know way to solve this problem?

    Thanks
    strcmp is case sensitive so you should use strcasecmp instead.

  3. #3
    SitePoint Addict
    Join Date
    Nov 2002
    Location
    Maryland
    Posts
    307
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That did it! Thanks so much ma201dq.


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
  •