Jump to content

Order an array of firstname lastname by lastname


xtiancjs

Recommended Posts

Hi ,

 

I have an array of values that contain a firstname and lastname.

 

$names = array('John Smith','Fred Johnson','Mark Williams'); 

 

How would i display them so they are ordered alpha by the last name?

I was thinking i could split each value into a $key=>$value pair and order the result by the $value. Not sure how (or if this is the right way) to go about this.

 

 

Link to comment
Share on other sites

Something like this should work:

 

 

function cmplast($a, $b){return strcmp(end(explode(' ', $a)), end(explode(' ', $b)));}$names = array('John Smith','Fred Johnson','Mark Williams');usort($names, 'cmplast');echo implode("<br />\n", $names);

 

 

Output:

 

 

Fred JohnsonJohn SmithMark Williams

 

Link to comment
Share on other sites

alexWD:

How would that work on a name like

Jon Van Warner

or a woman who also chooses to include her maiden name

Alice Touey Zanderfield

 

$array=array();

$names=array(
'mark warner',
'mark von warner',
'jon van meurker',
'janice morse code',
'arron mc moran',
'thom abbott');

$j=0;
foreach($names as $name)
{
  $n=explode(" ", $name);

  $first=array_shift($n);
  $ct= count($n);
  for($i=0;$i<$ct;$i++)
  {$last.=" {$n[$i]}";}
  $last=trim($last);
  $array[$j][first]=$first;
  $array[$j][last]=$last;
  $last='';
$j++;
}

foreach ($array as $key => $row) {
    $count[$key]  = $row['last'];
}
array_multisort($count, SORT_ASC, $array);
  echo "<pre>";
print_r($array);

 

xtiancjs:

The way you originally have your array make it impossible to differentiate between a two part last name an a middle name. You would be best to store names as:

$array(0=>

array(

'last'=>'Von warner',

'middle'=>'august',

'first'=>'jon'

)

);

Then use array_multisort on it. lots less code and no mistakes with middle/compound lasts

 

HTH

Teamatomic

Link to comment
Share on other sites

There won't be a perfect way to do this. I don't know what you're trying to do, but optimumly you should have the last name stored separately. You have no way to distinguish between, say, a two-word last name, or just a middle name and a last name. The example I provided was just a solution to the given example.

Link to comment
Share on other sites

This was my eventual solution to order tags in wordpress by the second word - in this case last name, and then split that into 3 columns - that seems to be working.

There is prob a more concise way to write this. Thanks for the responses.

 

<h2><?php
//empty arrays 
$firstn = array();
$lastn = array();
$lastn2 = array();
$link = array();


//populate the above arrays
$tags = get_tags();
foreach ($tags as $tag ) {
$piece = $tag->slug;
$pieces = explode("-",$piece);
array_push($firstn,$pieces[0]);
array_push($lastn,$pieces[1]);
array_push($lastn2,$pieces[2]);
array_push($link, get_tag_link ($tag->term_id));
}

//extract values of arrays and sort alpha by last name
$firstname = array_values($firstn);
$lastname = array_values($lastn);
asort($lastname);
$lastname2 = array_values($lastn2);
$taglinks = array_values($link);
//new array that is combination of first, last and link
$all  = array();
//populate $all
foreach($lastname as $key=>$value) {
$all[$value][0] = $value;
$all[$value][1] = $firstname[$key];
$all[$value][2] = $taglinks[$key];
$all[$value][3] = $lastname2[$key];
//count number of names
$number  = count($all);
// get number for chunk value
$chunk_value = round($number / 3) ;
}
//array of 3 columns
$col = array();
//split into 3 
$col = array_chunk($all, $chunk_value, true);
//column 1 values
$column1 = array_values($col[0]);
//colum 2 values
$column2 = array_values($col[1]);
//column 3 values
$column3 = array_values($col[2]);
//display content
?> 
<table cellpadding="0" cellspacing="0" border="0" width="100%">
<tr>
<td valign="top"><?php foreach ($column1 as $key=>$value) {
echo '<a href="'. $value[2] .'" rel="tag">'.
$value[1].' '.$value[0].' '.$value[3].'</a><br />' ;
} ?></td><td valign="top"><?php foreach ($column2 as $key=>$value) {
echo '<a href="'. $value[2] .'" rel="tag">'.
$value[1].' '.$value[0].' '.$value[3].'</a><br />' ;
} ?></td>
<td valign="top"><?php foreach ($column3 as $key=>$value) {
echo '<a href="'. $value[2] .'" rel="tag">'.
$value[1].' '.$value[0].' '.$value[3].'</a><br />' ;
} ?></td></tr>
</table>

  </h2>

Link to comment
Share on other sites

This was my eventual solution to order tags in wordpress by the second word - in this case last name, and then split that into 3 columns - that seems to be working.

There is prob a more concise way to write this. Thanks for the responses.

 

<h2><?php
//empty arrays 
$firstn = array();
$lastn = array();
$lastn2 = array();
$link = array();


//populate the above arrays
$tags = get_tags();
foreach ($tags as $tag ) {
$piece = $tag->slug;
$pieces = explode("-",$piece);
array_push($firstn,$pieces[0]);
array_push($lastn,$pieces[1]);
array_push($lastn2,$pieces[2]);
array_push($link, get_tag_link ($tag->term_id));
}

//extract values of arrays and sort alpha by last name
$firstname = array_values($firstn);
$lastname = array_values($lastn);
asort($lastname);
$lastname2 = array_values($lastn2);
$taglinks = array_values($link);
//new array that is combination of first, last and link
$all  = array();
//populate $all
foreach($lastname as $key=>$value) {
$all[$value][0] = $value;
$all[$value][1] = $firstname[$key];
$all[$value][2] = $taglinks[$key];
$all[$value][3] = $lastname2[$key];
//count number of names
$number  = count($all);
// get number for chunk value
$chunk_value = round($number / 3) ;
}
//array of 3 columns
$col = array();
//split into 3 
$col = array_chunk($all, $chunk_value, true);
//column 1 values
$column1 = array_values($col[0]);
//colum 2 values
$column2 = array_values($col[1]);
//column 3 values
$column3 = array_values($col[2]);
//display content
?> 
<table cellpadding="0" cellspacing="0" border="0" width="100%">
<tr>
<td valign="top"><?php foreach ($column1 as $key=>$value) {
echo '<a href="'. $value[2] .'" rel="tag">'.
$value[1].' '.$value[0].' '.$value[3].'</a><br />' ;
} ?></td><td valign="top"><?php foreach ($column2 as $key=>$value) {
echo '<a href="'. $value[2] .'" rel="tag">'.
$value[1].' '.$value[0].' '.$value[3].'</a><br />' ;
} ?></td>
<td valign="top"><?php foreach ($column3 as $key=>$value) {
echo '<a href="'. $value[2] .'" rel="tag">'.
$value[1].' '.$value[0].' '.$value[3].'</a><br />' ;
} ?></td></tr>
</table>

  </h2>

 

You can use strrpos($str, ' ') to find the position of the last space in your string. Then use this in conjunction with substr to display certain parts of the string. Let me know if you need help with how to do this.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.