PHP从具有ALMOST重复值的多维数组中删除特定行

时间:2021-12-18 21:31:45

I have a multidimensional array in which it can happen that two rows are ALMOST duplicates, but not completely. For example:

我有一个多维数组,其中可能发生两行是ALMOST重复,但不完全。例如:

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?

它们仅在['seit']值上有所不同。当发生类似这样的事情时,我想从数组中删除具有最高['seit']值的行。我怎样才能做到这一点?

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.

编辑:好的,多一点信息。对不起,我只是觉得这就够了。此数组包含有关其他用户正在等待的用户的信息。他们等待的时间是几天。他们等待特定用户的日期可以通过两种方式给出:1。当某些条件适用时,它将自动设置。 2.可以手动设置日期。有时可能会给用户一个自动日期,但也有一个手动日期。

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.

但是我必须在不知道任何这些值的情况下找到这些双重值。我也不知道双值会出现在哪个索引处。如果两个'username'列具有相同的值,我需要遍历数组,比较每一行。如果他们有,它必须查看行的'seit'列以找出哪一行具有更高的'seit'值。然后删除此行。

I just kinda cannot wrap my head around how to code that.

我只是有点无法解决如何编码。

EDIT2: Some more code:

EDIT2:更多代码:

    // 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.

因此,当我合并$ manual和$ user时,可能会发生同一个用户在两个数组中,因为他们有curr = 1 AND manual = 1.例如我只是在第一个MySQL语句中添加“AND manual = 0”我得到一个“数组大小不一致”的问题,所以我只想去删除天数更高的行。名字是独一无二的。

1 个解决方案

#1


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.

http://repl.it/nUQ - 一个非常粗略的例子。我会留给你把它推断成你可以在你的环境中应用的东西。

$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');

#1


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.

http://repl.it/nUQ - 一个非常粗略的例子。我会留给你把它推断成你可以在你的环境中应用的东西。

$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');