<?php
$q = "SELECT `Code` FROM `productspecs` GROUP BY `Code` HAVING COUNT(1) > 1";
$r = mysqli_query($conn, $q);
$a = 0;
while ($row = mysqli_fetch_array($r))
{
echo $row['Code'];
$q2 = "SELECT * FROM `productspecs` WHERE `Code`='" . $row['Code'] . "'";
$r2 = mysqli_query($conn, $q2);
$i = 0;
while ($row2 = mysqli_fetch_array($r2))
{
$Mod = $row2['ModificationDate'];
$ModDates[] = DateTime::createFromFormat('d/m/Y', $Mod); // === $ModDates[$i]
$ModDates[$i]['SpecID']=$row2['SpecID'];
$i++;
}
usort($ModDates); // <<< ??
$DeleteIDs[] = $ModDates[0]['SpecID'];
foreach ($ModDates as $ModDate)
{
echo $ModDate->format('d-m-Y');}
unset($ModDates); // unset the array for the next code
echo '</br>';
echo 'Delete: ' . $DeleteIDs[$a];
$a++;
}
?>
Hi I'm trying to Delete specifications from the product database (or at least get a list of ones to delete) where we have duplicates and we want to delete the oldest one.
So here I've 1) found a list of duplicate codes 2) Assigned spec id to each mod date 3) Sorted the mod dates to find the oldest*NOTE 4) Found a list of ids to delete by finding the id assigned to the first of the sorted array. 5) unsets the moddate array for the next set of duplicates
NOTE: I think solving this problem might have something to do with sorting the multidimensional array - this is the part that I'm fairly sure is wrong. If this wasn't a multidimensional array we would at least see the modification dates in order. At this point I COULD just do a search for product code and oldest (first in array) mod date which thinking about it now - would achieve the same result? Does it matter that the primary key will be lost - after all - all I need is to get rid of the product code with the older modification date.. it doesn't matter that we don't have the primary key?
P.S. I did see a post about sorting multi-dimensional arrays Here but I felt I should show you what I was doing in case somebody had a better suggestion?