算法:二维数组去重

2021-06-21 02:05

阅读:498

标签:实现   图片   inf   tis   ring   duplicate   foreach   origin   rgb   

1、需求
待授权设备中存在同名则重命名,以保证授权设备名称在数据库中的唯一性

2、逻辑图

技术图片

 

 3、代码

private function deduplicateDeviceName(&$dataset)
{
    if (!($sameset = $this->getDeviceSameset($dataset))) {
        return false;
    }
    foreach ($sameset as $originalDeviceName => &$partialSameset) {
        foreach ($partialSameset as $key => &$name) {
            static $flag = 0;
            ++$flag;
            $name .= ‘(‘ . $flag . ‘)‘;
            unset($key);
        }
        $this->isExistInDatabase($dataset, $partialSameset, $originalDeviceName, $flag);
        $flag = 0;
        unset($originalDeviceName);
    }
    unset($sameset);
    // 按设备名称升序排列
    $sortByNameLowercase = array_map(‘strtolower‘, array_column($dataset, ‘server_name‘));
    array_multisort($sortByNameLowercase, SORT_ASC, SORT_STRING, $dataset);
}

private function getDeviceSameset($dataset)
{
    if (empty($dataset)) {
        return false;
    }

    $sameset = [];
    $repeatTimes = array_count_values(array_column($dataset, ‘server_name‘));
    foreach ($dataset as $key => $item) {
        if (!isset($item[‘server_name‘], $repeatTimes[$item[‘server_name‘]])) {
            continue;
        }
        if (
            $repeatTimes[$item[‘server_name‘]] $this->pluto_device->name_exists($item[‘server_name‘], null, false)
        ) {
            continue;
        }
        $device = $item[‘server_name‘];
        $sameset[$device][$key] = $device;
        unset($device, $key, $item);
    }

    return $sameset;
}

/**
 * 设备名是否存在于数据库中
 *
 * @todo 取数据库中已存在的设备名后缀作为$flag,保证设备名称后缀的连续性
 * @param array $dataset 原始待授权设备
 * @param array $partialSameset 按同名分组后的设备
 * @param string $originalDeviceName 同名设备原始名称
 * @param int $flag 重命名设备用的数字后缀标标记
 * @return bool|null
 */
private function isExistInDatabase(&$dataset, &$partialSameset, $originalDeviceName, $flag)
{
    if (!$this->pluto_device->name_exists(array_values($partialSameset), null, false)) {
        foreach ($partialSameset as $key => $name) {
            $dataset[$key][‘server_name‘] = $name;
            unset($key, $name);
        }
        return false;
    }
    foreach ($partialSameset as $key => &$name) {
        ++$flag;
        $name = $originalDeviceName . ‘(‘ . $flag . ‘)‘;
        $dataset[$key][‘server_name‘] = $name;
    }
    $this->isExistInDatabase($dataset, $partialSameset, $originalDeviceName, $flag);
}

4、实现效果

database.pluto_device list

947645681   oracle-6-9          4   1   192.168.8.119   1541158513
3374036272  oracle-7-0          4   1   192.168.8.118   1541158505
1591878207  WIN-DKD4QL4TDAA(2)  4   2   192.168.8.120   1541384920
3867940323  oracle-7-0(4)       4   2   192.168.8.117   1542091640
1179277971  oracle-7-0(1)       4   2   192.168.8.116   1542091693
199481921   oracle-7-0(2)       4   2   192.168.8.115   1542091732

waitingAuthDevice list

$dataset = [
    [
        ‘ip‘ => ‘192.168.8.100‘,
        ‘platform‘ => ‘1‘,
        ‘server_name‘ => ‘oracle-7-0‘,
    ],
    [
        ‘ip‘ => ‘192.168.8.101‘,
        ‘platform‘ => ‘2‘,
        ‘server_name‘ => ‘WIN-DKD4QL4TDAA‘,
    ],
    [
        ‘ip‘ => ‘192.168.8.102‘,
        ‘platform‘ => ‘3‘,
        ‘server_name‘ => ‘oracle-7-0‘,
    ],
    [
        ‘ip‘ => ‘192.168.8.103‘,
        ‘platform‘ => ‘4‘,
        ‘server_name‘ => ‘WIN-DKD4QL4TDAA‘,
    ],
    [
        ‘ip‘ => ‘192.168.8.104‘,
        ‘platform‘ => ‘5‘,
        ‘server_name‘ => ‘oracle‘,
    ],
    [
        ‘ip‘ => ‘192.168.8.105‘,
        ‘platform‘ => ‘6‘,
        ‘server_name‘ => ‘oracle-6-9‘,
    ],
    [
        ‘ip‘ => ‘192.168.8.106‘,
        ‘platform‘ => ‘7‘,
        ‘server_name‘ => ‘oracle-7-0‘,
    ],
];
var_dump($this->deduplicateDeviceName($dataset));
Array
(
    [0] => Array
        (
            [ip] => 192.168.8.104
            [platform] => 5
            [server_name] => oracle
        )

    [1] => Array
        (
            [ip] => 192.168.8.105
            [platform] => 6
            [server_name] => oracle-6-9(1)
        )

    [2] => Array
        (
            [ip] => 192.168.8.100
            [platform] => 1
            [server_name] => oracle-7-0(7)
        )

    [3] => Array
        (
            [ip] => 192.168.8.102
            [platform] => 3
            [server_name] => oracle-7-0(8)
        )

    [4] => Array
        (
            [ip] => 192.168.8.106
            [platform] => 7
            [server_name] => oracle-7-0(9)
        )

    [5] => Array
        (
            [ip] => 192.168.8.101
            [platform] => 2
            [server_name] => WIN-DKD4QL4TDAA(3)
        )

    [6] => Array
        (
            [ip] => 192.168.8.103
            [platform] => 4
            [server_name] => WIN-DKD4QL4TDAA(4)
        )

)

 

算法:二维数组去重

标签:实现   图片   inf   tis   ring   duplicate   foreach   origin   rgb   

原文地址:https://www.cnblogs.com/gentsir/p/14906982.html


评论


亲,登录后才可以留言!