Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am looking for a way I can remove duplicate values from a PHP array from a resulting MySQL query. What I have so far gives me the query I want but shows any duplicates consistantly. I need to remove them for a navigation menu. Any help would be appreciated.

This is what I have so far...

$c_search = mysql_query("SELECT * FROM members WHERE interest1 LIKE '%$input%' or interest2 LIKE '%$input%' or interest3 LIKE '%$input%'");
$array = Array();
while($rows = mysql_fetch_assoc($c_search)) {
array_push($array, $rows['interest1']);
array_push($array, $rows['interest2']);
array_push($array, $rows['interest3']);
for($i=0; $i<count($array); $i++) {
echo $array[$i] . "\n";
}
}

I have tried changing the MySQL query to use DISTINCT, no luck. I tried using array_unique($array, SORT_STRING), no luck. Not sure what elese to try. Please help, thanks.

share|improve this question
The problem is probably with your sql query – MrFoh Apr 21 '12 at 23:37
Give an example of your final array and your desired array. – sberry Apr 21 '12 at 23:37
Why isn't array_unique working? That's odd. – Taha Paksu Apr 21 '12 at 23:38
DISTINCT would do the trick but depends on where you used it. you need something like SELECT DISTINCT interest1, DISTINCT interest2... FROM ..... – l̕aͨŵƦȆ̴̟̟͙̞ͩ͌͝ƞCͭ̏ȇ ƇhƐȓ0nè Apr 21 '12 at 23:40
$c_search = mysql_query("SELECT DISTINCT interest1 FROM members WHERE interest1 LIKE '%$input%'"); while($rows = mysql_fetch_assoc($c_search)) { $result = $rows['interest1']; echo $result . "\n"; } this works for one row query – itgeek25 Apr 21 '12 at 23:46
show 1 more comment

1 Answer

up vote 0 down vote accepted

One of the options: create your arrays manually, performing check if (!in_array($array))

share|improve this answer
I didn't even think of this. Still kinda new to php. But this is great. Also, I did check my MySQL query and it wasn't working. I didn't test it first, too much faith in myself. The DISTINCT works that Lawrence Cherone mentioned. Thanks guys for all your help and quicks responses. – itgeek25 Apr 22 '12 at 0:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.