根据条件对数组进行计数:分步指南
P粉133321839
P粉133321839 2023-09-10 13:15:11
0
2
584

我有一个带有接口的主机列表。 该代码必须计算每个主机的重复接口。 最后,代码还必须为每个主机显示重复的接口 X 次。

我问这个问题是因为我想发送 X 主机有一个或多个失败接口发生故障的警报。

$data = array(
    array("192.168.0.1","eth1"),
    array("192.168.0.2","eth2"),
    array("192.168.0.3","eth3"),
    array("192.168.0.1","eth1"),
    array("192.168.0.4","eth1"),
    array("192.168.0.2","eth5")
);

我在这里遵循了其他示例,但大多数都是简单数组,或者如果是多维示例,则示例不相似。

我已经尝试过了...

<?php
$data = array(
    array("192.168.0.1","eth1"),
    array("192.168.0.2","eth2"),
    array("192.168.0.3","eth3"),
    array("192.168.0.1","eth1"),
    array("192.168.0.4","eth1"),
    array("192.168.0.2","eth5")
);


$counter_data = count($data);

$duplicated_host = array_filter(array_count_values(array_column($data, 0)), function($v) { return $v > 1; });
print_r($duplicated_host);
print ("<br>");

$duplicated_host_keys = (array_keys($duplicated_host));

for ($row_num = 0; $row_num < $counter_data; $row_num++)
{
    $host = $data[$row_num][0];
    $interface = $data[$row_num][1];
    if (in_array($host,$duplicated_host_keys))
    {
        print($host . " " . $interface . "<br>");
    }
    
}

上面的代码是错误的,有些工作,但它不是我所期望的...... 有没有简单的方法可以做到这一点?

最后的输出应如下所示:

Host 192.168.0.1 has eth1 repeated 2 times. --> For current data only
Host 192.168.0.1 has eth9 repeated 5 times.
Host 192.168.0.4 has eth1 repeated 9 times.

P粉133321839
P粉133321839

全部回复(2)
P粉124070451

这可能就是您正在寻找的:

<?php
$input = array(
    array("192.168.0.1","eth1"),
    array("192.168.0.2","eth2"),
    array("192.168.0.3","eth3"),
    array("192.168.0.1","eth1"),
    array("192.168.0.4","eth1"),
    array("192.168.0.2","eth5"),
);
$output = [];
array_walk($input, function($entry) use (&$output) {
    [$host, $interface] = $entry;
    if (isset($host, $output) && isset($interface, $output[$host])) {
        $output[$host][$interface]++;
    } else {
        $output[$host][$interface] = 1;
    }
});
print_r($output);

输出为:

Array
(
    [192.168.0.1] => Array
        (
            [eth1] => 2
        )
    [192.168.0.2] => Array
        (
            [eth2] => 1
            [eth5] => 1
        )
    [192.168.0.3] => Array
        (
            [eth3] => 1
        )
    [192.168.0.4] => Array
        (
            [eth1] => 1
        )
)
P粉706038741

您需要对两个分组进行分组,首先是主机,然后是接口。

然后您可以循环此分组数组以显示/发送输出:

<?php

$data = array(
    array("192.168.0.1","eth1"),
    array("192.168.0.2","eth2"),
    array("192.168.0.3","eth3"),
    array("192.168.0.1","eth1"),
    array("192.168.0.4","eth1"),
    array("192.168.0.2","eth5")
);


$result = [];

foreach ($data as $arr) {
    [ $host, $nic ] = $arr;
    if (!isset($result[$host])) {
        $result[$host] = [];
    }
    if (!isset($result[$host][$nic])) {
        $result[$host][$nic] = 0;
    }

    $result[$host][$nic]++;
}


foreach ($result as $host => $nics) {
    foreach ($nics as $nic => $count) {
        echo "${host} has his '${nic}' interface fail ${count} time(s)" . PHP_EOL;
    }
}
192.168.0.1 has his 'eth1' interface fail 2 time(s)
192.168.0.2 has his 'eth2' interface fail 1 time(s)
192.168.0.2 has his 'eth5' interface fail 1 time(s)
192.168.0.3 has his 'eth3' interface fail 1 time(s)
192.168.0.4 has his 'eth1' interface fail 1 time(s)

在线试用!


NIC -->“网络接口卡”

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板