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
7
  • 2
    Depending on your data and the table structure it might be more efficient to change your SQL query to only return unique data. Commented Jan 20, 2015 at 11:13
  • The thing is doing that, I would be forced to create two querys - I think. Commented Jan 20, 2015 at 11:14
  • What makes something a duplicate? Same Id, same date or same everything? Commented Jan 20, 2015 at 11:16
  • 1
    I think you are using array_unique incorrectly. it removes all the duplicates from the array. As far as I understood you are trying to detect if array has duplicates or not. is that right? Commented Jan 20, 2015 at 11:17
  • You should add sample data to your question, otherwise it's just guesswork. Commented Jan 20, 2015 at 11:17

4 Answers 4

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;


}
Sign up to request clarification or add additional context in comments.

Comments

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));
}

Comments

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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.