wer0ckz Posted March 22, 2011 Share Posted March 22, 2011 Hi. Please help me. I need an example of codes maybe 4-6 lines: example for code for small program that sorts the following string, eliminating duplicates. The string is: a, b, rt, tril, drv, b, span, drv, art, arc, d, a, drc Thank you Quote Link to comment https://forums.phpfreaks.com/topic/231351-help-sorting-strings-and-eliminating-duplicates/ Share on other sites More sharing options...
kenrbnsn Posted March 22, 2011 Share Posted March 22, 2011 We don't write code for you. We help you with code you've already written. This and the other post you made look like homework problems. Ken Quote Link to comment https://forums.phpfreaks.com/topic/231351-help-sorting-strings-and-eliminating-duplicates/#findComment-1190710 Share on other sites More sharing options...
QuickOldCar Posted March 22, 2011 Share Posted March 22, 2011 I had some free time. <?php $string = "a, b, rt, tril, drv, b, span, drv, art, arc, d, a, drc"; $string_array = explode(", ",$string); asort($string_array); $string_array = array_unique($string_array); foreach ($string_array as $string_single){ $string_single = trim($string_single); echo "$string_single<br />"; } ?> Results would be: a arc art b d drc drv rt span tril Quote Link to comment https://forums.phpfreaks.com/topic/231351-help-sorting-strings-and-eliminating-duplicates/#findComment-1190716 Share on other sites More sharing options...
kenrbnsn Posted March 22, 2011 Share Posted March 22, 2011 Since someone already answered you, here's another way: <?php $str = 'a, b, rt, tril, drv, b, span, drv, art, arc, d, a, drc'; $ary = array_unique(explode(', ',$str)); sort($ary); echo implode("<br>\n",$ary) . "<br>\n"; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/231351-help-sorting-strings-and-eliminating-duplicates/#findComment-1190722 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.