Rumah  >  Soal Jawab  >  teks badan

Mengira tatasusunan berdasarkan syarat: panduan langkah demi langkah

Saya mempunyai senarai hos dengan antara muka. Kod mesti mengira antara muka pendua untuk setiap hos. Akhir sekali, kod juga mesti memaparkan antara muka pendua X kali untuk setiap hos.

Saya bertanya soalan ini kerana saya ingin menghantar makluman bahawa hos X telah gagal dengan satu atau lebih antara muka yang gagal.

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

Saya telah mengikuti contoh lain di sini, tetapi kebanyakannya adalah tatasusunan mudah, atau jika ia contoh berbilang dimensi, contohnya tidak serupa.

Saya sudah mencubanya...

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

Kod di atas adalah salah, ia agak berfungsi tetapi ia tidak seperti yang saya jangkakan... Adakah terdapat cara mudah untuk melakukan ini?

Keluaran akhir sepatutnya kelihatan seperti ini:

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粉133321839P粉133321839426 hari yang lalu562

membalas semua(2)saya akan balas

  • P粉124070451

    P粉1240704512023-09-11 12:09:10

    Ini mungkin yang anda cari:

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

    Keluarannya ialah:

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

    balas
    0
  • P粉706038741

    P粉7060387412023-09-11 09:48:22

    Anda perlu mengumpulkan dua kumpulan, pertama hos dan kemudian antara muka.

    Anda kemudian boleh mengulangi tatasusunan berkumpulan ini untuk memaparkan/menghantar 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)
    

    Cuba dalam talian!


    NIC --> "Kad Antara Muka Rangkaian"

    balas
    0
  • Batalbalas