0

The below code is generating several of the below error and it's pointing to

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

?>

The seems to point to $Array = array_merge($Array, array_combine($newkey, $newval));

I can't seem to figure out the issue.

Below is the array from the snmp3_real_walk(), printout of $data after assigning the return from snmp3_real_walk() function.

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

Instead of assigning $data to the snmp_walk function return and create an array like the below, I don't have any issue.

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

Can you please help?

KodeFor.Me
  • 13,069
  • 27
  • 98
  • 166
Mike T
  • 47
  • 3
  • 1
    You should compare the return value of `snmp3_real_walk()` to the array you expect it to return. Use `var_dump()` on both, and see where they differ. If I mock the array from the `print_r()` output, then your code runs without warnings or errors. I have not checked it for any potential logic issues. – Ro Achterberg Nov 05 '20 at 07:34
  • 1
    From the [array_combine() documentation](https://www.php.net/manual/en/function.array-combine.php): _"Returns the combined array, FALSE if the number of elements for each array isn't equal."_ Since the two arrays are of different length, `array_combine()` returns NULL, and when you merge that with `$Array` you get NULL, which you are then storing back in `$Array`, which then causes problems on the next iteration. – kmoser Nov 05 '20 at 07:36

1 Answers1

0

Unfortunately, your English description is not really good, and I am not 100% sure of what you want to do.

So far, what I understood is that the function snmp3_real_walk returns back to you an array with the following 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"
)

But when you run the foreach look you get too many warnings of type:

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

Based on the data you shared, I have written the following code, that is running smoothly, and without warnings:

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

The output of the above code on my local environment is like that:

enter image description here

So, if still have errors, then maybe you have to check what is the actual output of the function snmp3_real_walk, because it may return some unexpected results.

Hope this code helps you.

KodeFor.Me
  • 13,069
  • 27
  • 98
  • 166
  • Thank you KodeFor.me, kmoser, and Ro Achterberg. I see the issue. I didn't post the complete array return for snmp3_real_walk function. I just dumped and noticed that one of the return has [ETRA-VRTR-MIB::vRtrIfName.2.24] => STRING: "to-vprn-2:1218". And my code has $newval = explode(' ',trim($val, 'STRING :')); which should retain only the interface name. However since there is another ":" char in the interface name, it doesn't retain the interface name. Trying to figure out how to retain only "to-vprn-2:1218". If anyone can help, I would appreciate. – Mike T Nov 05 '20 at 14:11
  • 1
    Did you consider using regular expressions to match what you need? Maybe this way is simpler to get the information you want. – KodeFor.Me Nov 05 '20 at 14:35
  • Thanks. Got it to work using regular expression. $re = '/STRING: ([^ ]+)/'; preg_match($re, $val, $newval); $newval = array($newval[1]); – Mike T Nov 05 '20 at 16:30
  • @MikeT if you got an answer and you want, don't forget to update or mark the answer as the right on using the checkmark below the voting controls :) Thank you in advance ! :) – KodeFor.Me Nov 05 '20 at 16:34
  • 1
    Thank you KodeFor.Me :) I'd thought I'd done that. Just did it. – Mike T Nov 05 '20 at 21:20