0

I have been trying to manage duplicate data which is shown to users.

I thought I can add the varibales to arrays and use the function array_unique

I want to be able to manage the rows which contain a duplicate date and split them into different sections for example

if(duplicate.exists == true)
{
 //do something to the duplicate row
}
else
{
//do something to the row which isnt a duplicate
}

I cant figure out why array_unique is not working.

Help would be appreciated, Thanks.

$result = mysqli_query($con, "SELECT *
        FROM quotes order by DATE asc ");

        $index1 = array();
        $fact1 = array(); 
        $newDate1 = array();

        while ($row = mysqli_fetch_array($result)) {

            $index = $row['id'];
            $dbdate = $row['date'];
            $fact = $row['quote'];

            $newDate = date("d-m-Y", strtotime($dbdate));

            $index1[] = $fact;
            $fact1[] = $fact;
            $newDate1[] = $newDate;

        }

Then have a function which loops through each array and finds out if a certain date has already exists.

for($i=0; $i<count($index1); $i++) {

 echo(array_unique($newDate1));

}

 else
{

}

Thats an example of the data that will be in the DB. It's the id, fact, date example 1, fact, 2015-01-22

1 Steve Jobs unveiled the first Apple #Mac computer and changed technology forever (1984) - 2015-01-24
2 In 2011, the Urban Technology Innovation Center was launched in New York City - 2015-01-25
3 #Bebo was launched a whole decade ago today (2005), who feels old? - 2015-01-26
4 Sun Microsystems was acquired by Oracle Corporation for $7.4 bn (2010) - 2015-01-27
Hashey100
  • 994
  • 5
  • 22
  • 47

4 Answers4

2

Considering you are sorting your query on date and that makes something a duplicate, all you need to do is track the last date.

$lastdate = '';
while ($row = mysqli_fetch_array($result)) {
    $dbdate = $row['date'];

    if ($lastdate==$dbdate) {
      //duplicate
    } else {
      //first or unique
    }

    $lastdate = $dbdate;


}
Hugo Delsing
  • 13,803
  • 5
  • 45
  • 72
1

It can be quicker to do this in SQL

Find the duplicates

SELECT * FROM quotes GROUP BY `date` HAVING COUNT(`date`) > 1 order by DATE asc

Find the non-duplicates

SELECT * FROM quotes GROUP BY `date` HAVING COUNT(`date`) = 1 order by DATE asc
user1977661
  • 233
  • 2
  • 10
1

So as noted by the OP, he wants a way to detect duplicates and not remove them.

To detect duplicates you can use something like this, answered in another question.

I would prefer this:

function array_has_dupes($array) {
   return count($array) !== count(array_unique($array));
}
Community
  • 1
  • 1
Headshota
  • 21,021
  • 11
  • 61
  • 82
1

Use SQL "count" and "group".

 create table z (x varchar(100),y varchar(100));
 insert into z values ('a','b');
 insert into z values ('a','b');
 insert into z values ('a','c');
 select x,y,count(*) as count from z group by x,y;

You get values:

+------+------+-------+ | x | y | count | +------+------+-------+ | a | b | 2 | | a | c | 1 | +------+------+-------+

And use it in php code.

Mikrobi
  • 341
  • 1
  • 4