phpsnmparray-mergearray-combine

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


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?


Solution

  • 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.