Results 1 to 5 of 5

Thread: Removing Duplicate Rows in an Array without array_unique()

  1. #1
    Join Date
    Mar 2009
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Removing Duplicate Rows in an Array without array_unique()

    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>

  2. #2
    Join Date
    Sep 2008
    Location
    Midland, Texas
    Posts
    52
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default

    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.

  3. #3
    Join Date
    Mar 2009
    Posts
    65
    Thanks
    13
    Thanked 4 Times in 4 Posts

    Default

    It's always better to tweak the SQL because it is more flexible.

    I think you may need to put array_unique here

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

    More help here

    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

    PHP Code:
    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

    PHP Code:
    for ($i 0$i count($output); $i++)
    {
    $output[i] = array_unique($output[i]);


  4. #4
    Join Date
    Mar 2009
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    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.

  5. #5
    Join Date
    Mar 2009
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Thank you your help anyway.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •