Heylance Posted April 24, 2008 Share Posted April 24, 2008 I know this is probably beginner stuff, but so am I. I have 3 items in an array an I want the one with the longest string length. Example: print_r ($city_list) outputs: (Array [0] => Catalina [1] => Catalina Tucson [2] => Tucson) Is there a way to sort by srtlen, small to large as its the largest that I need or if it is on the end That's about it . Thanks Quote Link to comment https://forums.phpfreaks.com/topic/102678-solved-finding-the-biggest-needle-in-haystack/ Share on other sites More sharing options...
micah1701 Posted April 24, 2008 Share Posted April 24, 2008 there maybe an easier way, but my first thought is to recreate the array and use each value's length as its array key. then you can sort the array by the keys. something like: <?php $exisiting_array = array("Catalina", "Catalina Tucson", "Tucson"); $new_array = array(); foreach($exisiting_array as $key => $word){ $new_key = strlen($word); $new_array[$new_key] = $word; } ksort($new_array); echo "<pre>\n"; print_r($new_array); echo "</pre>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/102678-solved-finding-the-biggest-needle-in-haystack/#findComment-525855 Share on other sites More sharing options...
Heylance Posted April 25, 2008 Author Share Posted April 25, 2008 Thanks micah1701 I'm kinda following what you suggested. Would that make the new array look like: ([6] => Tucson [8] => Catalina [15] => Catalina Tucson) The problem is that I don't know what the $word is, only that there is an array. I think I may have been going about what I need to get the wrong way or asked the question the wrong way. This is what I need The array is $city_list ([0] => Catalina [1] => Catalina Tucson [2] => Tucson) or it could be ([0] => Catalina [1] => Tucson [2] => Tucson Catalina) if($city_list > 1) $city_name = the value with the longest string length ( which would be "Catalina Tucson" or "Tucson Catalina" ) Things to consider (if important): 1. if the array only contains 2 values ($city_list[0] & $city_list[1]), then $city_list[1] will always be the longest string 2. there may be more than 3 values in the array. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/102678-solved-finding-the-biggest-needle-in-haystack/#findComment-527460 Share on other sites More sharing options...
doni49 Posted April 26, 2008 Share Posted April 26, 2008 With the following $city_name will contain the longest string. <?php $l = 0; foreach($city_list as $city){ if(strlen($city) > $l){ //if $city is longer than $l change $l to the length of $city //and set $city_name to $city $l = strlen($city); $city_name = $city; } } ?> EDIT: What do you want to do if there is more than one city with the same length--and it happens to be the longest length in the list? Quote Link to comment https://forums.phpfreaks.com/topic/102678-solved-finding-the-biggest-needle-in-haystack/#findComment-527527 Share on other sites More sharing options...
sasa Posted April 26, 2008 Share Posted April 26, 2008 try <?php function compare_len($a, $b){ return strlen($b)-strlen($a); } $arr = array('a','aaa','aa','bbb'); usort($arr, 'compare_len'); print_r($arr); ?> Quote Link to comment https://forums.phpfreaks.com/topic/102678-solved-finding-the-biggest-needle-in-haystack/#findComment-527581 Share on other sites More sharing options...
Heylance Posted April 26, 2008 Author Share Posted April 26, 2008 Perfect Thanks doni49 I tried it with 3 and with 4 values in the array and works great. To answer your edit... Sample: cities table 1. Casa 2. Benson 3. North 4. North Casa 5. North Tucson Casa 6. Starr 7. Tucson input: "North Tucson Casa" explode input Get from DB anything that equals the exploded parts or forward combinations of exploded parts = Casa, North, North Tucson Casa, Tucson There will always be 1 value with a longer strlen. If there is some situation that you think I am missing pertaining to your "edit", please let me know so I can explore it. Thanks again.... Lance --------------- BTW micah1701 I figured out what $word is. It is $city_list[0] and your code worked with the result being what I guessed it would be and it put the longest strlen at the end of the array. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/102678-solved-finding-the-biggest-needle-in-haystack/#findComment-527583 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.