Forum Moderators: coopster

Message Too Old, No Replies

Eliminating Duplicates

Query clean up

         

bumpaw

4:47 am on Nov 21, 2005 (gmt 0)

10+ Year Member



I am pulling from Category in thousands of rows in a table and the results are strings delimited by "\". I explode() the strings as they loop out and get some clean arrays called $chop. After explode() I would like to have no duplicate values.
[3]
$cat_query = mysql_query($Csql) or die(mysql_error());
// Count the rows
$cat_num = mysql_num_rows($cat_query);
if($cat_num > 0) {
$x=0;
while($cat = mysql_fetch_array($cat_query)) {
$x=$x+1;
$chop = explode("\\",$cat["Category"]);

echo "$chop[0] $chop[1] $chop[2] $chop[3] $chop[4] <br />";

}[/3]

PRESENT OUTPUT:
Disposables Coffee Service Coffee
Disposables Coffee Service Coffee Stirrers
Disposables Coffee Service Filters
Disposables Coffee Service Unwrapped
Disposables Coffee Service Wrapped

DESIRED OUTPUT:
Disposables
Coffee Service
Coffee
Filters
Unwrapped
Wrapped

dreamcatcher

9:27 am on Nov 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try array_unique()


$cat_query = mysql_query($Csql) or die(mysql_error());
// Count the rows
$cat_num = mysql_num_rows($cat_query);
if($cat_num > 0) {
$x=0;
while($cat = mysql_fetch_array($cat_query)) {
$x=$x+1;
$chop = explode("\\",$cat["Category"]);
$chop = array_unique($chop);
}
foreach ($chop as $chopped)
{
echo $chopped."<br />";
}

Although I may have read that wrong about what you want.

dc

bumpaw

3:31 pm on Nov 21, 2005 (gmt 0)

10+ Year Member



This doesn't seem to work. I am trying to come up with a list of unique names for a long navigation menu. As I cycle through each array the goal is to save only new values as in the example I posted.

dreamcatcher

4:23 pm on Nov 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Right, I think I got it. Try this:


$new = array();
$cat_query = mysql_query($Csql) or die(mysql_error());
// Count the rows
$cat_num = mysql_num_rows($cat_query);
if($cat_num > 0) {
$x=0;
while($cat = mysql_fetch_array($cat_query)) {
$x=$x+1;
$chop = explode("\\",$cat["Category"]);

for ($i=0; $i<count($chop); $i++)
{
if (!in_array($chop[$i], $new))
{
$new[] = $chop[$i];
}
}
foreach ($new as $cats)
{
echo $cats."<br>";
}

Does that work?

dc

bumpaw

6:35 pm on Nov 21, 2005 (gmt 0)

10+ Year Member



Not yet but the approach seems right with in_array().
It seems to be in an endless loop now. I really appreciate your help and if you need I can sticky you with a link for you to see the actual output.

dreamcatcher

7:27 pm on Nov 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ah, try unsetting the $chop variable after each loop.


$new = array();
$cat_query = mysql_query($Csql) or die(mysql_error());
// Count the rows
$cat_num = mysql_num_rows($cat_query);
if($cat_num > 0) {
$x=0;
while($cat = mysql_fetch_array($cat_query)) {
$x=$x+1;
$chop = explode("\\",$cat["Category"]);

for ($i=0; $i<count($chop); $i++)
{
if (!in_array($chop[$i], $new))
{
$new[] = $chop[$i];
}
unset ($chop);
}
foreach ($new as $cats)
{
echo $cats."<br>";
}

dc