xtiancjs Posted February 26, 2010 Share Posted February 26, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/193475-order-an-array-of-firstname-lastname-by-lastname/ Share on other sites More sharing options...
Alex Posted February 26, 2010 Share Posted February 26, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/193475-order-an-array-of-firstname-lastname-by-lastname/#findComment-1018561 Share on other sites More sharing options...
teamatomic Posted February 26, 2010 Share Posted February 26, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/193475-order-an-array-of-firstname-lastname-by-lastname/#findComment-1018569 Share on other sites More sharing options...
Alex Posted February 26, 2010 Share Posted February 26, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/193475-order-an-array-of-firstname-lastname-by-lastname/#findComment-1018573 Share on other sites More sharing options...
xtiancjs Posted February 26, 2010 Author Share Posted February 26, 2010 Thanks guys, Unfortunately i have no control over how the original names array is created. From the code posted i think i can work it out - will post resulting code here as a reply. Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/193475-order-an-array-of-firstname-lastname-by-lastname/#findComment-1018603 Share on other sites More sharing options...
xtiancjs Posted March 1, 2010 Author Share Posted March 1, 2010 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> Quote Link to comment https://forums.phpfreaks.com/topic/193475-order-an-array-of-firstname-lastname-by-lastname/#findComment-1019853 Share on other sites More sharing options...
aeroswat Posted March 1, 2010 Share Posted March 1, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/193475-order-an-array-of-firstname-lastname-by-lastname/#findComment-1019869 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.