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.

share|improve this question
    
While looping thru arrays, store values from previous array index. Now compare it with current values of current array index. If two or 3 values of old array index match with current , remove it. Getting? – Pratik C Joshi May 10 '15 at 16:49

Assuming your array structure is fairly consistent, you can loop over the array, comparing like values and recording the index with the highest comparative value so far. This question is a bit broad though, as you've not given a lot of information about your code. If you'd like a more concise answer, you're going to need to do a better job of explaining your problem, with more code examples, and things you've tried.

http://repl.it/nUQ - A very rough example. I'll leave it to you to extrapolate this into something you can apply in your environment.

$multi = Array(
    Array(
        'name' => 'bob',
        'age' => '42',
        'id' => 412),
    Array(
        'name' => 'bob',
        'age' => 42,
        'id' => 624),
    Array(
        'name' => 'jim',
        'age' => 35,
        'id' => 800)
);


function remove_highest($arr, $iden, $comp) {
    $highest = 0;
    $row = -1;

    foreach ($arr as $key=>$value) {
        if (isset($value[$iden[0]]) &&
            $value[$iden[0]] === $iden[1] ) {
            if ($value[$comp] > $highest) {
                $highest = $value[$comp];
                $row = $key;
            }
        }
    }

    if ($row > -1) {
        array_splice($arr, $row, 1);
    }

    return $arr;
}

$current = remove_highest($multi, ['name', 'bob'], 'id');
share|improve this answer
    
I added a bit of text to my question to explain. Your example is a good starting point for me, though. – Khalliys May 10 '15 at 19:45
    
Could you provide us with a larger data sample and more code? Are usernames unique? – Oka May 10 '15 at 20:40
    
Added it to the question. Yes, usernames are unique, there wouldn't be a problem with that. – Khalliys May 10 '15 at 21:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.