I have a list of hosts with interfaces. The code must count the duplicate interfaces for each host. Finally, the code must also display the duplicate interface X times for each host.
I'm asking this question because I want to send alerts that host X has failed with one or more failed 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've followed other examples here, but most are simple arrays, or if it's a multidimensional example, the examples are not similar.
I already tried...
<?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, it works somewhat but it's not what I expected... Is there an easy way to do this?
The final output should look like this:
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.
This might be what you are looking for:
The output is:
You need to group two groups, first the host and then the interface.
You can then loop over this grouped array to display/send the output:
Try it online!
NIC -->"Network Interface Card"