根據條件對數組進行計數:逐步指南
P粉133321839
P粉133321839 2023-09-10 13:15:11
0
2
534

我有一個有介面的主機清單。 該程式碼必須計算每個主機的重複介面。 最後,程式碼也必須為每個主機顯示重複的介面 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 -->「網路介面卡」

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!