PHP 警告:array_combine() 和 array_merge() 生成警告

PHP Warning: array_combine() and array_merge() generating warning

下面的代码产生了下面的几个错误,它指向

PHP Warning: array_merge(): Expected parameter 1 to be an array, null given in /home/mike/snmpCode.php on line 20

PHP Warning: array_combine(): Both parameters should have an equal number of elements in /home/mike/snmpCode.php on line 20

#!/usr/bin/php
<?PHP

$data = snmp3_real_walk (
    'localhost',
    'User3',
    'authPriv',
    'MD5',
    'pwd123',
    'DES',
    'pwd123',
    'ETRA-VRTR-MIB::vRtrIfName'
);

print_r($data);

$Array = array();

foreach($data as $key => $val) {
    $newval = explode(':',trim($val, 'STRING: '));
    $newkey = explode(' ',trim($key, '[ETRA-VRTR-MIB::vRtrIfName.]'));
    $Array = array_merge($Array, array_combine($newkey, $newval));
}

$data = $Array;

print_r($data);

?>

似乎指向$Array = array_merge($Array, array_combine($newkey, $newval));

我似乎无法弄清楚这个问题。

下面是 snmp3_real_walk() 中的数组,在从 snmp3_real_walk() 函数分配 return 后打印 $data。

Array
(
    [ETRA-VRTR-MIB::vRtrIfName.1.1] => STRING: "intf1"
    [ETRA-VRTR-MIB::vRtrIfName.1.2] => STRING: "intf2"
    [ETRA-VRTR-MIB::vRtrIfName.1.3] => STRING: "intf3"
    [ETRA-VRTR-MIB::vRtrIfName.1.4] => STRING: "intf4"
    [ETRA-VRTR-MIB::vRtrIfName.1.6] => STRING: "intf5"
    [ETRA-VRTR-MIB::vRtrIfName.1.7] => STRING: "intf6"
    [ETRA-VRTR-MIB::vRtrIfName.1.8] => STRING: "intf7"
    [ETRA-VRTR-MIB::vRtrIfName.1.9] => STRING: "intf8"
    [ETRA-VRTR-MIB::vRtrIfName.1.10] => STRING: "intf9"
    [ETRA-VRTR-MIB::vRtrIfName.1.11] => STRING: "intf10"
    [ETRA-VRTR-MIB::vRtrIfName.1.12] => STRING: "intf11"
    [ETRA-VRTR-MIB::vRtrIfName.1.13] => STRING: "intf12"
    [ETRA-VRTR-MIB::vRtrIfName.1.14] => STRING: "intf13"
    [ETRA-VRTR-MIB::vRtrIfName.1.15] => STRING: "intf14"
    [ETRA-VRTR-MIB::vRtrIfName.1.19] => STRING: "intf15"
    [ETRA-VRTR-MIB::vRtrIfName.1.39] => STRING: "intf16"
    [ETRA-VRTR-MIB::vRtrIfName.1.64] => STRING: "intf17"
)

而不是将 $data 分配给 snmp_walk 函数 return 并创建如下所示的数组,我没有任何问题。

$data = array(
    '[ETRA-VRTR-MIB::vRtrIfName.1.1]' => 'STRING: "intf1"', 
    '[ETRA-VRTR-MIB::vRtrIfName.1.2]' => 'STRING: "intf2"'
);

你能帮忙吗?

不幸的是,你的英文描述不是很好,我不能 100% 确定你想做什么。

到目前为止,我的理解是函数 snmp3_real_walk return 返回给您一个包含以下数据的数组:

Array
(
    [ETRA-VRTR-MIB::vRtrIfName.1.1] => STRING: "intf1"
    [ETRA-VRTR-MIB::vRtrIfName.1.2] => STRING: "intf2"
    [ETRA-VRTR-MIB::vRtrIfName.1.3] => STRING: "intf3"
    [ETRA-VRTR-MIB::vRtrIfName.1.4] => STRING: "intf4"
    [ETRA-VRTR-MIB::vRtrIfName.1.6] => STRING: "intf5"
    [ETRA-VRTR-MIB::vRtrIfName.1.7] => STRING: "intf6"
    [ETRA-VRTR-MIB::vRtrIfName.1.8] => STRING: "intf7"
    [ETRA-VRTR-MIB::vRtrIfName.1.9] => STRING: "intf8"
    [ETRA-VRTR-MIB::vRtrIfName.1.10] => STRING: "intf9"
    [ETRA-VRTR-MIB::vRtrIfName.1.11] => STRING: "intf10"
    [ETRA-VRTR-MIB::vRtrIfName.1.12] => STRING: "intf11"
    [ETRA-VRTR-MIB::vRtrIfName.1.13] => STRING: "intf12"
    [ETRA-VRTR-MIB::vRtrIfName.1.14] => STRING: "intf13"
    [ETRA-VRTR-MIB::vRtrIfName.1.15] => STRING: "intf14"
    [ETRA-VRTR-MIB::vRtrIfName.1.19] => STRING: "intf15"
    [ETRA-VRTR-MIB::vRtrIfName.1.39] => STRING: "intf16"
    [ETRA-VRTR-MIB::vRtrIfName.1.64] => STRING: "intf17"
)

但是当您 运行 foreach 查看时,您会收到太多类型的警告:

PHP Warning: array_merge(): Expected parameter 1 to be an array, null given in /home/mike/snmpCode.php on line 20

PHP Warning: array_combine(): Both parameters should have an equal number of elements in /home/mike/snmpCode.php on line 20

根据你分享的数据,我写了下面的代码,运行ning顺利,没有警告:

// I have comment out this code on my local, because I don't have 
// the implementation you use on your own development environment:
//
// $data = snmp3_real_walk (
//     'localhost',
//     'User3',
//     'authPriv',
//     'MD5',
//     'pwd123',
//     'DES',
//     'pwd123',
//     'ETRA-VRTR-MIB::vRtrIfName'
// );

// By this code, I have simulated the output of the data you have provide 
// in your example:
$data = [
    "ETRA-VRTR-MIB::vRtrIfName.1.1" => "STRING: intf1",
    "ETRA-VRTR-MIB::vRtrIfName.1.2" => "STRING: intf2",
    "ETRA-VRTR-MIB::vRtrIfName.1.3" => "STRING: intf3",
    "ETRA-VRTR-MIB::vRtrIfName.1.4" => "STRING: intf4",
    "ETRA-VRTR-MIB::vRtrIfName.1.6" => "STRING: intf5",
    "ETRA-VRTR-MIB::vRtrIfName.1.7" => "STRING: intf6",
    "ETRA-VRTR-MIB::vRtrIfName.1.8" => "STRING: intf7",
    "ETRA-VRTR-MIB::vRtrIfName.1.9" => "STRING: intf8",
    "ETRA-VRTR-MIB::vRtrIfName.1.10" => "STRING: intf9",
    "ETRA-VRTR-MIB::vRtrIfName.1.11" => "STRING: intf10",
    "ETRA-VRTR-MIB::vRtrIfName.1.12" => "STRING: intf11",
    "ETRA-VRTR-MIB::vRtrIfName.1.13" => "STRING: intf12",
    "ETRA-VRTR-MIB::vRtrIfName.1.14" => "STRING: intf13",
    "ETRA-VRTR-MIB::vRtrIfName.1.15" => "STRING: intf14",
    "ETRA-VRTR-MIB::vRtrIfName.1.19" => "STRING: intf15",
    "ETRA-VRTR-MIB::vRtrIfName.1.39" => "STRING: intf16",
    "ETRA-VRTR-MIB::vRtrIfName.1.64" => "STRING: intf17"
];
$Array = [];

foreach($data as $key => $val) {
    $newval = explode(':',trim($val, 'STRING: '));
    $newkey = explode(' ',trim($key, '[ETRA-VRTR-MIB::vRtrIfName.]'));

    // By the following if statement, I try to eliminate the warnings by 
    // checking if both values exist.
    if ( $newkey && $newval ) {
        $newArray = array_combine($newkey, $newval);

        // Finally to further eliminate the warnings, I try to make sure that the
        // $newArray has been created.
        if ( $newArray ) {
            $Array = array_merge($Array, $newArray);
        }
    }
}

$data = $Array;

print_r($data);

以上代码在我本地环境下的输出是这样的:

所以,如果仍然有错误,那么也许你必须检查函数 snmp3_real_walk 的实际输出是什么,因为它可能 return 一些意想不到的结果。

希望这段代码对您有所帮助。