Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

PHP sort names by last and first names

Posted on 2009-04-18
8
Medium Priority
?
584 Views
Last Modified: 2013-12-13
The code I have explodes the names into alphabetical order but I need to get it in alphabetical order by last name... note the Mr.

Any help would be appreciated.
<?PHP
$harry_potter_characters = "Bathsheda Babbling, Malcolm Baddock, Mr Bagman, Ludovic Bagman, " .
                                   "Otto Bagman, Millicent Bagnold, Bathilda Bagshot, Heathcote, Barbary, " .
                                   "Musidora Barkwith, Baruffio, Ali Bashir, Hetty Bayliss, Oswald Beamish, " .
                                   "Herbert Beery, Flavius Belby, Marcus Belby, Humphrey Belcher, Katie Bell";
 
class potter_character
{
   var $first_name;
   var $last_name;
}
 
$potter_characters=explode(", " , $harry_potter_characters);
usort($potter_characters, "strcasecmp");
var_dump($potter_characters)
?>

Open in new window

0
Comment
Question by:scottkern3
  • 4
  • 4
8 Comments
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24177094
Try this:
usort($potter_characters, create_function('$a,$b',
  '$tmpa=explode(" ",$a);
   $tmpb=explode(" ",$b);
   return strcasecmp(
     array_pop($tmpa),
     array_pop($tmpb));'));

Open in new window

0
 
LVL 1

Author Comment

by:scottkern3
ID: 24177508
This is the output I get from that...
I was thinking the output should be:

Babbling, Bathsheda
Baddock, Malcolm....
...followed by first name only characters

hopefully that makes sense.
0 => string 'Bathsheda Babbling' (length=18)
  1 => string 'Malcolm Baddock' (length=15)
  2 => string 'Otto Bagman' (length=11)
  3 => string 'Ludovic Bagman' (length=14)
  4 => string 'Mr Bagman' (length=9)
  5 => string 'Millicent Bagnold' (length=17)
  6 => string 'Bathilda Bagshot' (length=16)
  7 => string 'Barbary' (length=7)
  8 => string 'Musidora Barkwith' (length=17)
  9 => string 'Baruffio' (length=8)
  10 => string 'Ali Bashir' (length=10)
  11 => string 'Hetty Bayliss' (length=13)
  12 => string 'Oswald Beamish' (length=14)
  13 => string 'Herbert Beery' (length=13)
  14 => string 'Marcus Belby' (length=12)
  15 => string 'Flavius Belby' (length=13)
  16 => string 'Humphrey Belcher' (length=16)
  17 => string 'Katie Bell' (length=10)
  18 => string 'Heathcote' (length=9)

Open in new window

0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24177547
Is this what you want:
foreach($potter_characters as $name) {
  $nameparts = explode(' ',$name);
  echo array_pop($nameparts);
  if($nameparts)
    echo ', '.implode(' ',$nameparts);
  echo '<br />';
}

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Author Comment

by:scottkern3
ID: 24177577
Nice! Now the only thing is the first names onlys at the end of the list.

Is that even possible?

0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24177600
Sure, just modify the sorting function. If there are multiple names, sort before those with a single name:
usort($potter_characters, 
  create_function('$a,$b',
  '$tmpa=explode(" ",$a);
   $tmpb=explode(" ",$b);
   $lasta=array_pop($tmpa);
   $lastb=array_pop($tmpb);
   return strcasecmp(
     (count($tmpa)?"1":"2").$lasta,
     (count($tmpb)?"1":"2").$lastb);'));

Open in new window

0
 
LVL 1

Author Comment

by:scottkern3
ID: 24177667
Your code before this last one had the output with last name, first name... <--- I liked this
this put it back to full name with last name alphabetical like in this (what I pasted in code snippet)

Is there a way I could do it with the "for_each" example above?
 
0 => string 'Bathsheda Babbling' (length=18)
  1 => string 'Malcolm Baddock' (length=15)
  2 => string 'Otto Bagman' (length=11)
  3 => string 'Ludovic Bagman' (length=14)
  4 => string 'Mr Bagman' (length=9)
  5 => string 'Millicent Bagnold' (length=17)
  6 => string 'Bathilda Bagshot' (length=16)
  7 => string 'Musidora Barkwith' (length=17)
  8 => string 'Ali Bashir' (length=10)
  9 => string 'Hetty Bayliss' (length=13)
  10 => string 'Oswald Beamish' (length=14)
  11 => string 'Herbert Beery' (length=13)
  12 => string 'Marcus Belby' (length=12)
  13 => string 'Flavius Belby' (length=13)
  14 => string 'Humphrey Belcher' (length=16)
  15 => string 'Katie Bell' (length=10)
  16 => string 'Barbary' (length=7)
  17 => string 'Baruffio' (length=8)
  18 => string 'Heathcote' (length=9)

Open in new window

0
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 200 total points
ID: 24177675
The last code snippet from me was just the sorting, it does not do output.
<?PHP
$harry_potter_characters = "Bathsheda Babbling, Malcolm Baddock, Mr Bagman, Ludovic Bagman, " .
                           "Otto Bagman, Millicent Bagnold, Bathilda Bagshot, Heathcote, Barbary, " .
                           "Musidora Barkwith, Baruffio, Ali Bashir, Hetty Bayliss, Oswald Beamish, " .
                           "Herbert Beery, Flavius Belby, Marcus Belby, Humphrey Belcher, Katie Bell";
 
$potter_characters=explode(", " , $harry_potter_characters);
 
usort($potter_characters, 
  create_function('$a,$b',
  '$tmpa=explode(" ",$a);
   $tmpb=explode(" ",$b);
   $lasta=array_pop($tmpa);
   $lastb=array_pop($tmpb);
   return strcasecmp(
     (count($tmpa)?"1":"2").$lasta,
     (count($tmpb)?"1":"2").$lastb);'));
 
foreach($potter_characters as $name) {
  $nameparts = explode(' ',$name);
  echo array_pop($nameparts);
  if($nameparts)
    echo ', '.implode(' ',$nameparts);
  echo '<br />';
}
 
?>

Open in new window

0
 
LVL 1

Author Comment

by:scottkern3
ID: 24177695
That makes a little more sense.

Thank you so much!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo‚Ķ
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

580 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