I have a multidimensional array in which it can happen that two rows are ALMOST duplicates, but not completely. For example:
Array ( ...
[2] => Array ( [username] => Akando [grpNo] => 5 [away] => 0 [manudate] => 1428357600 [seit] => 12 )
...
[7] => Array ( [username] => Akando [grpNo] => 5 [away] => 0 [manudate] => 1428357600 [seit] => 33 ) )
They differ only in the ['seit'] value. When something like this happens I would like to remove the row with the highest ['seit'] value from the array. How can I do that?
EDIT: Okay, a bit more info. I'm sorry, I just thought that would be enough. This array contains info about users that other users are waiting for. How long they are waiting is given in days. The date since when they are waiting for a specific user can be given in two ways: 1. it will be set automatically when certain conditions apply. 2. the date can be set manually. Sometimes it can happen that a user is given an automatic date, but also a manual date.
In this case we have a double entry of one user in the array, with the only difference being the days. How I get to all this is quite complicated, so it would be easier to just remove the row with the higher days number afterwards than to avoid having them in the beginning.
But I have to find these double values without knowing any of them. I also won't know at which index the double values will appear. I would need to iterate through the array, comparing for each row if two 'username' columns have the same value. And if they have it must look at the row's 'seit' column to find out which row has the higher 'seit' value. And then remove this row.
I just kinda cannot wrap my head around how to code that.
EDIT2: Some more code:
// Get the users we are waiting for automatically
$result = $db->query("SELECT username, grpNo, away, manudate FROM bb".$n."_counter WHERE curr = '1'");
while($row=$db->fetch_array($result))
{
$user[] = $row;
}
// Sort $user DESC
foreach ($user as $key => $node)
{
$gruppe[$key] = $node['grpNo'];
}
array_multisort($gruppe, SORT_DESC, $user);
$user = array_map("unserialize", array_unique(array_map("serialize", $user)));
// Turn date into days since today
for($i = 0; $i < count($user); $i++)
{
$user[$i]['seit'] = floor((time() - $max[$i]['seit']) / (60 * 60 * 24));
}
// ---------------------------------
// Set dates manually
$result = $db->query("SELECT username, grpNo, away, manudate FROM bb".$n."_counter WHERE manual = '1'");
while($row=$db->fetch_array($result))
{
$manual[] = $row;
}
if(!empty($manual))
{
for($i = 0; $i < count($manual); $i++)
{
$manual[$i]['seit'] = floor((time() - $manual[$i]['manudate']) / (60 * 60 * 24));
}
// merge $manual and $user
if(isset($manual))
{
$user = array_merge_recursive($user, $manual);
}
}
So, when I merge $manual and $user it might happen that the same user is in both arrays, because they have curr = 1 AND manual = 1. If I for example just add "AND manual = 0" to the first MySQL statement I get a "Array sizes are inconsistent" problem, so therefore I just want to go and remove the row where the number of days is higher. The names are unique.