如何使用黑名单数组删除值,然后减少剩余值以消除差距? [英] How to remove values using a blacklist array, then reduce the remaining values to eliminate gaps?

查看:34
本文介绍了如何使用黑名单数组删除值,然后减少剩余值以消除差距?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题基本上是我上一个问题的延伸:

This question is basically an extension of my previous question:

如何减去数组的值但仍在位置

我有一个数组输入数组。每个子数组中的值始终由以 0 开头的值组成,并且没有任何间隙,这些值将递增1。但是,值不一定是按顺序排列的,在执行所需的逻辑时,我需要保留此顺序。

I have an input array of arrays. The values in each subarray always consist of values starting from 0 and without any gaps, values are incremented by one. However, the values are not necessarily in order AND I need to preserve this order while executing my required logic.

接下来,我有一个黑名单,希望从所有子数组中删除。必须删除黑名单数组中存在的所有原始子数组值。

Next I have a blacklist of values that I wish to remove from all subarrays. Any original subarray value that exists in the blacklist array must be removed.

数组的示例输入数组:

$arrays = [
    [0, 3, 10, 5, 6, 9, 2, 7, 1, 4, 8, 11],
    [0, 1, 2, 3],
    [0, 5, 2, 4, 3, 1],
    [0, 1, 3, 2]
];

样本黑名单数组:

$deletes = [3, 5];

我想要的输出是:

[
    [0, 8, 4, 7, 2, 5, 1, 3, 6, 9],
    [0, 1, 2],
    [0, 2, 3, 1],
    [0, 1, 2],
]

所有剩余值均大于 3 减少了 1 ,且值大于 5 减少了 2 ,因为我删除了 2 个数字。

All remaining values greater than 3 are reduced by 1 and values greater than 5 are reduced by 2 since I deleted 2 numbers.

如果所有数字都在给定的子数组小于黑名单数组中的所有数字,则无需更改该子数组。

If all numbers in a given subarray are less than all of the numbers in the blacklist array, then no change is required for that subarray.

我在这里尝试进行编码 https://3v4l.org/lX2MP ,但是返回它们的值时我卡住了。所有数组值都在组合。

I have a coding attempt here https://3v4l.org/lX2MP, but I am stuck when returning their values. All array values were combining.

推荐答案

我将稍微更改第二,第三和第四子数组,以更好地演示其行为。

I am going to alter your 2nd, 3rd and 4th subarrays slightly to better demonstrate the behavior.

利用我对上一个问题的回答中的一种技术,我实际上只是将代码逻辑包装在附加循环中 array_map()

Drawing upon one of the techniques in my answer to your previous question, I am effectively only just wrapping the code logic in an additional loop array_map().

array_diff()用于立即销毁输入数组中与删除值匹配的所有值。然后在输入数组的其余值上使用 array_reduce()来迭代和减少生成的消除间隙的整数。

array_diff() is used to instantly destroy any values in the input array that match the deletes values. Then array_reduce() is use on the remaining values of the input array to iterate and reduce the integers any generated eliminate gaps.

Inside array_reduce()中,您将看到 $ value> $ item 。此比较将返回 true false 。当布尔值用作数字时, true 变为 1 false 变为 0 。基本上,我是从 $ value 0 1 >取决于每个 $ deletes 值与给定的 $ value 的比较方式。

Inside of array_reduce(), you will see $value > $item. This comparison will return true or false. When a boolean value is used as a number, true becomes 1 and false becomes 0. Basically, I am either subtracting 0 or 1 from $value depending on how each $deletes value compares to the given $value.

例如,在处理 10 时, 10 大于 3 ,因此它变为 9 ,而 10 大于 5 ,因此 9 变为 8

As a specific example, when processing 10, 10 is larger than 3, so it becomes 9, and 10 is larger than 5 so 9 becomes 8.

这是全部完成而无需对数据进行预排序。

This is all done without needing to pre-sort the data.

代码:(演示

$arrays = [[0, 3, 10, 5, 6, 9, 2, 7, 1, 4, 8, 11], [0, 1, 2, 3], [0, 5, 2, 4, 3, 1], [0, 1, 3, 2]];
$deletes = [3, 5];

var_export(
    array_map(
        function($array) use ($deletes) {
            $result = [];
            foreach (array_diff($array, $deletes) as $value) {
                $result[] = array_reduce(
                    $deletes,
                    function ($carry, $item) use ($value) {
                        return $carry - ($value > $item);
                    },
                    $value
                );
            }
            return $result;
        },
        $arrays
    )
);

这里是一种替代方式,其行为方式相同,但并不太依赖函数式编程:(演示

Here's an alternative that behaves the same way but doesn't rely on functional programming so much: (Demo)

foreach ($arrays as $index => $array) {
    $filtered = array_diff($array, $deletes);  // destroy blacked values
    foreach ($filtered as $value) {
        $originalValue = $value;
        foreach ($deletes as $delete) {
            $value -= $originalValue > $delete; // reduce to eliminate gaps
        }
        $result[$index][] = $value;
    }
}

var_export($result);

输出(对于任一片段):

Output (for either snippet):

array (
  0 => 
  array (
    0 => 0,
    1 => 8,
    2 => 4,
    3 => 7,
    4 => 2,
    5 => 5,
    6 => 1,
    7 => 3,
    8 => 6,
    9 => 9,
  ),
  1 => 
  array (
    0 => 0,
    1 => 1,
    2 => 2,
  ),
  2 => 
  array (
    0 => 0,
    1 => 2,
    2 => 3,
    3 => 1,
  ),
  3 => 
  array (
    0 => 0,
    1 => 1,
    2 => 2,
  ),
)

这篇关于如何使用黑名单数组删除值,然后减少剩余值以消除差距?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆