Return unique results from exploded array (NEED HELP FAST PLEASE)


#1

Hi,

Ok, I have returned a field from my database named “description” and have done so using DISTINCT as to only get unique values.

This outputs something like “Vauxhall Corsa 1.2 SXI” and if there were 2 like that it would only get one which is what I wanted, however I am now building search fields and need the “Make” field to only return the Make of each car, so “Vauxhall” and the problem being that even though these 2 are unique…

Vauxhall Corsa 1.2 SXI
Vauxhall Astra 1.4

I need to only return the make so “Vauxhall”.

I used an explode function to only return the first word “Vauxhall” and put this in a while loop as to return the make for each car. Problem is now that I have…

Vauxhall
Vauxhall

And I need to onlydisplay 1 instance of each word and “array_unique” doesn’t seem to be having any effect.

Here is the chunk of mentioned code…

[php]
$sql = “SELECT DISTINCT * FROM database”;
$query = mysql_query($sql);
while ($search = mysql_fetch_array($query))
{
$explode = explode(" ", trim($search[‘description’]));
echo “<option class = “make”>”;
echo $explode[0];
echo “”;
}
[/php]

PLEASE help urgently!

Thank you.


#2

Just like SQL, you can use UNIQUE in PHP. Here is the link that explains it. Look at the samples down the page a little… Hope that helps…
http://php.net/manual/en/function.array-unique.php