搜索2d数组并与php中的数据库进行比较

时间:2017-05-22 08:32:08

标签: php mysql multidimensional-array wamp mac-address

我有一个看起来像这样的2D数组:



    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
            )

    )




我通过在我的交换机上使用snmpwalk获得了这些。[1] =>数组:是与上面的mac地址相关联的端口号。 我需要将这些mac地址与保存在数据库中的mac地址进行比较:



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




如何获取数据库中没有的mac地址以及与mac地址对应的端口号? 结果看起来像这样:



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




3 个答案:

答案 0 :(得分:0)

为什么不创建一个看起来像这样的数组?

Array(
  [0] => Array(
      ['mac'] => '0026B9AC17A0',
      ['port'] => 25
  ),
  [1] => Array(
      ['mac'] => '080027042B7C',
      ['port'] => 25
  ),
  [2] => Array(
      ['mac'] => 'B439D6388380',
      ['port'] => 0
  )
  ...
)

答案 1 :(得分:0)

您可以尝试以下代码:Google Maps Polyline Utility。最终你可以删除空的或奇怪的字符。

<?php

$dbArray = [
        "0026B9AC17A0",
        "008064A890B2", 
        "1866DA3440E9",
        "3464A9D5B334", 
        "5C260A4F8B7F", 
        "0050568A6F7B"
];

$resultArray = [
  [
                "0024E8D4915C",
                "0026B9AC17A0",
                "080027042B7C",
                "1866DA0EBD4C",
                "",
                "",
                "64006A5F5DE0",
                "B05ADACE88BC",
                "B439D6388380",
                "D067E51A3B4F",
                "D4BED9C2B47F",
                "D4C9EFB500A3",
                "M000000",
                "F48E382F5B06"
  ],
  [ "25", "25", "25", "25", "25", "25", "25", "25", "0", "25", "25", "25", "25", "25"]
];
echo "mac address: \t port number:\n";
foreach($resultArray[0] as $key=> $value){
  if(!in_array($value, $dbArray)){
    echo $value ." \t ". $resultArray[1][$key] ."\n";
  } 
}

答案 2 :(得分:0)

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