Jump to content

Remove non unique words


only1perky

Recommended Posts

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.

 

Link to comment
Share on other sites

<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>

Link to comment
Share on other sites

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>";

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.