only1perky Posted October 30, 2008 Share Posted October 30, 2008 Hi guys, I hope someone can help me with this as it driving me crazy. I pull a list of values from my table to use in a drop down box. This is working fine. The problem is I want to remove the non unique words when it displays them. For example: I have a list of football competitions like: FA Cup FA Cup Qualifying Round 1 FA Cup Qualifying Round 2 FA Cup Qualifying Round 3 FA Cup Round 1 FA Cup Round 2 Carling Cup Carling Cup Round 1 Carling Cup Round 2 What I would like to be displayed is simply: FA Cup Carling Cup Can anyone help me out on how to achieve this please before I go insane. Cheers in advance. Quote Link to comment https://forums.phpfreaks.com/topic/130732-remove-non-unique-words/ Share on other sites More sharing options...
effigy Posted October 30, 2008 Share Posted October 30, 2008 <pre> <?php $cups = array( 'FA Cup', 'FA Cup Qualifying Round 1', 'FA Cup Qualifying Round 2', 'FA Cup Qualifying Round 3', 'FA Cup Round 1', 'FA Cup Round 2', 'Carling Cup', 'Carling Cup Round 1', 'Carling Cup Round 2', ); $seen = array(); foreach ($cups as $cup) { $cup = preg_replace('/(?<=\bCup)\b.+$/', '', $cup); if (array_key_exists($cup, $seen)) { continue; } $seen[$cup] = null; echo $cup, "\n"; } ?> </pre> Quote Link to comment https://forums.phpfreaks.com/topic/130732-remove-non-unique-words/#findComment-678433 Share on other sites More sharing options...
Psycho Posted October 30, 2008 Share Posted October 30, 2008 If all of your events end with the word 'Cup' then what Effigy posted will work - If not, then you need to determine what rule to apply - perhaps removing ' Round *' or ' Qualifying *' from all of the values. However the foreach loop is not necessary. You can use an array with preg_replace(). I would suggest the following that would do the same thing with one line: $rounds = array( 'FA Cup', 'FA Cup Qualifying Round 1', 'FA Cup Qualifying Round 2', 'FA Cup Qualifying Round 3', 'FA Cup Round 1', 'FA Cup Round 2', 'Carling Cup', 'Carling Cup Round 1', 'Carling Cup Round 2', ); $events = array_unique(preg_replace('/( Qualifying)? Round(.)*/', '', $rounds)); echo "<pre>"; print_r($events); echo "</pre>"; Quote Link to comment https://forums.phpfreaks.com/topic/130732-remove-non-unique-words/#findComment-678449 Share on other sites More sharing options...
only1perky Posted October 30, 2008 Author Share Posted October 30, 2008 That's great thanks guys and it is working for now, however is there a way that if a different cup is entered that isn't listed it already (sometime in the future) it will be able to do it automatically? Maybe I'm trying to do too much but if it can be done automatically then that'll be a lot easier. Quote Link to comment https://forums.phpfreaks.com/topic/130732-remove-non-unique-words/#findComment-678571 Share on other sites More sharing options...
DeanWhitehouse Posted October 30, 2008 Share Posted October 30, 2008 That will require a database Quote Link to comment https://forums.phpfreaks.com/topic/130732-remove-non-unique-words/#findComment-678573 Share on other sites More sharing options...
shlumph Posted October 30, 2008 Share Posted October 30, 2008 You should organize your table so that Carling Cup and FA Cup are parent categories. Then, FA Cup Qualifying Round 1 would be a child category of FA Cup, etc. etc. Would kinda look like this: ID | Category | ParentID 1 FA Cup 0 2 FA Cup Qualifying Round 1 1 3 FA Cup Qualifying Round 2 1 4 FA Cup Qualifying Round 3 1 5 FA Cup Round 1 1 6 FA Cup Round 2 1 7 Carling Cup 0 8 Carling Cup Round 1 7 9 Carling Cup Round 2 7 Quote Link to comment https://forums.phpfreaks.com/topic/130732-remove-non-unique-words/#findComment-678581 Share on other sites More sharing options...
effigy Posted October 30, 2008 Share Posted October 30, 2008 $events = array_unique(preg_replace('/( Qualifying)? Round(.)*/', '', $rounds)); I would use /(?:\s+Qualifying)?\s+Round.*/. Quote Link to comment https://forums.phpfreaks.com/topic/130732-remove-non-unique-words/#findComment-678616 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.