Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to count an array with condition

I have a list of hosts with interface.
The code must count the repeated interfaces per host.
Also at the end the code must show the repeating interfaces X times, per host.

I am asking this because I want to send an alert that X host has a failing X interface or more failing interfaces.

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

I followed other examples in here, but most of them are for simple arrays or if multidimensional example then the examples aren’t similar.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I have tried this…

<?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>");
    }
    
}

The code above is wrong, somewhat working but it is not what I expect…
Is there a simple way to do this?

At the end the output should look like:

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.

>Solution :

You’ll need to group double, first host, then by interface.

Then you can looop over this grouping array to show/send the output:

<?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)

Try it online!


NIC –> ‘Network Interface Card’

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading