Heylance Posted May 9, 2008 Share Posted May 9, 2008 yup! it's official, not quite getting it. a little help please!!! I'm trying (again) to sort an array by the strlen of the values. This is a solution I got here before, but have run into a snag. $my_cars = array('chevy', 'chevys', 'dodge', 'dodges', 'lancer', 'mitsubishi', 'nissan'); if($my_cars){ foreach($my_cars as $key => $val){ $car = strlen($val); $car_list[$car] = $val; } } echo "<pre>\n"; print_r($car_list); echo "</pre>\n"; Array ( [5] => dodge [6] => nissan [10] => mitsubishi ) ksort($car_list); $car_list = array_reverse($car_list); echo "<pre>\n"; print_r($car_list); echo "</pre>\n"; Array ( [0] => mitsubishi [1] => nissan [2] => dodge ) Where did chevys, dodges and lancer go?? They got lost back in the foreach section. As you can see, I am trying to recreate the array and using the strlen to sort on. I was given this solution a few weeks ago. It seems that if any of the values are the same strlen, only the last one in the array is kept. What to do? Any suggestions.... Is it possible to do??? Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 9, 2008 Share Posted May 9, 2008 Yes, it is possible. The problem is that you are using the string length as the array index. Since 'chevys' and 'lancer' have the same string length the index of [6] first gets defined as 'chevys', then it gets redefined as 'lancers'. Here's one solution: <?php function sortByLength($a, $b) { if (strlen($a)==strlen($b)) { //If length =, sort by alpha return strcmp($a, $b); } return (strlen($a)>strlen($b)) ? 1 : -1; } $my_cars = array('chevy', 'chevys', 'dodge', 'dodges', 'lancer', 'mitsubishi', 'nissan'); if($my_cars){ usort($my_cars, "sortByLength"); } echo "<pre>\n"; print_r($my_cars); echo "</pre>\n"; ?> Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 9, 2008 Share Posted May 9, 2008 Here's another approach <?php $my_cars = array('chevy', 'chevys', 'dodge', 'dodges', 'lancer', 'mitsubishi', 'nissan'); if($my_cars){ foreach($my_cars as $key => $val){ $tmp_list[$val] = strlen($val); } asort($tmp_list); $my_cars = array_keys($tmp_list); } echo "<pre>\n"; print_r($my_cars); echo "</pre>\n"; ?> Quote Link to comment Share on other sites More sharing options...
Heylance Posted May 9, 2008 Author Share Posted May 9, 2008 Thanks mjdamato, Bingo, you made it look too easy! Your second solution was the easiest for me to get a grip on. I threw a little arsort it and Baaammm! Just what the doctor ordered. Thanks so much!!!! On the first one, I will have to study a little, which is good.. One question On the line: return (strlen($a)>strlen($b)) ? 1 : -1; What is the ? for? Remember I'm still a neeewwwbie. Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 9, 2008 Share Posted May 9, 2008 That is a shorthand version of an If/Else statement. I believe the correct name is the ternarry operator. Basically, that line states is the length of A is greater than the length of B return 1, else return -1. Here is a short article on it: http://php.codenewbie.com/articles/php/1480/The_Ternary_Operator-Page_1.html Personally, I woudl think the first example is more efficient, but I have not tested that hypothesis. Quote Link to comment 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.