Log in

View Full Version : Removing Duplicate Rows in an Array without array_unique()



Colonel_Popcorn
03-29-2009, 11:09 PM
Hey, I am working with a precarious situation here. I have finally gotten the code to search everything, but it returns duplicate values. I have tried using the array_unique() and just array() and even array_merge_recursive() but none of these have stopped the duplicate rows. When I use the array_unique() it will only return to me the first row and none others, or sometimes it won't return anything. Here is the code, I really hope someone can help me.

<html>
<body>

<?php
mysql_connect("connection", "username", "password") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());
$var=$_POST["search"];
$trimmed = trim($var);
$search_array = explode(" ",$trimmed);
if ($trimmed == "")
{
echo "<p>Please enter a search...</p>";
exit;
}
if (!isset($var))
{
echo "<p>We dont seem to have a search parameter!</p>";
exit;
}
$data=mysql_query("SELECT * FROM table");
$num_rows=mysql_num_rows($data);
$t=mysql_num_fields($data);
$row=array();
$num=1;

while($num<=$t)
{
foreach ($search_array as $search)
{
$fieldname=mysql_field_name($data,$num);
$results=mysql_query("SELECT * FROM table WHERE ($fieldname) LIKE '%$search%'");
while($r=mysql_fetch_array($results))
{
$row[]=array_merge($r, $row);
}
}
$num++;
}
$output=$row;
$rw=0;
while($rw<1000000000000000)
{
$col=1;
if ($output[$rw] == "")
{
}
else
{
while($col<=7)
{
echo $output[$rw][$col];
echo "<br>";
$col++;
}

?>
<img src="<?php echo $output[$rw][image];?>" />
<?php
echo "<br>";
}

$rw++;
}
echo "<br>";
echo "<br>";


?>
</body>
</html>

xtiano77
03-30-2009, 12:59 AM
If your table doesn't have too many columns, you might want to try changing your SQL query and use "DISTINCT" instead of "*". Hope this helps.

CrazyChop
03-31-2009, 04:50 PM
It's always better to tweak the SQL because it is more flexible.

I think you may need to put array_unique here



$fieldname=mysql_field_name($data,$num);
$results=mysql_query("SELECT * FROM table WHERE ($fieldname) LIKE '%$search%'");
while($r=mysql_fetch_array($results))
{
$r = array_unique($r);
$row[]=array_merge($r, $row);
}



More help here (http://sg.php.net/manual/en/function.array-unique.php)

The code may not work but you cannot use array_unique on the final array as it is a nested array. You need to use on each array within the containing array. You can do it outside too



foreach ($output as $row)
{
$row = array_unique($row);
}


Note: this works only for PHP 5 and above where foreach returns a reference. If not, you have to do a normal loop



for ($i = 0; $i < count($output); $i++)
{
$output[i] = array_unique($output[i]);
}

Colonel_Popcorn
03-31-2009, 08:59 PM
Never mind I was able to get it fixed by using a while loop and a descending number if statement to check to see if in the final array there were duplicates. It works and I understand it, that's all I need in the end.

Colonel_Popcorn
03-31-2009, 08:59 PM
Thank you your help anyway.