Search code examples
phpmysqlmultidimensional-arraywampmac-address

search 2d array and compare to database in php


i have a 2D array that looks like this :

    Array
    (
        [0] => Array
            (
                [0] => 0024E8D4915C
                [1] => 0026B9AC17A0
                [2] => 080027042B7C
                [3] => 1866DA0EBD4C
                [4] => " "\\&
    O��"
                [6] => 64006A5F5DE0
                [7] => B05ADACE88BC
                [8] => B439D6388380
                [9] => D067E51A3B4F
                [10] => D4BED9C2B47F
                [11] => D4C9EFB500A3
                [12] => "�M��>�"
                [13] => F48E382F5B06
            )

        [1] => Array
            (
                [0] =>  25
                [1] =>  25
                [2] =>  25
                [3] =>  25
                [4] =>  25
                [5] =>  25
                [6] =>  25
                [7] =>  25
                [8] =>  0
                [9] =>  25
                [10] =>  25
                [11] =>  25
                [12] =>  25
                [13] =>  25
            )

    )

i obtained these by using snmpwalk on my switch.[1]=>Array : is the port number that is associated with the mac addresses above. i need to compare these mac addresses against the mac addresses saved on the database which is :

    Array
    (
        [0] => 0026B9AC17A0
        [1] => 008064A890B2 
        [2] => 1866DA3440E9
        [3] => 3464A9D5B334 
        [4] => 5C260A4F8B7F 
        [5] => 0050568A6F7B
    )

How can i obtain the mac addresses thats not in the database along with the port number corresponding to the mac addresses? the result would look like this:

mac address:        port number:
[0] => 0024E8D4915C 25
[1] => 080027042B7C 25
[2] => 1866DA0EBD4C 25
[3] => " "\\&       25
    O��"
[4] => 64006A5F5DE0 25
[5] => B05ADACE88BC 25
[6] => B439D6388380 0
[7] => D067E51A3B4F 25
[8] => D4BED9C2B47F 25
[9] => D4C9EFB500A3 25
[10] => "�M��>�" 25
[11] => F48E382F5B06 25


Solution

  • //suppose your array's name is $array
    
    for ($i=0; $i < count($array[0]); $i++) { 
        $res[] = array(
            'mac' => $array[0][$i],
            'port' => $array[1][$i]
        );
    }
    
    print_r($res);