Jump to content

I am well stuck and need help!!!


fanta1066

Recommended Posts

So I am struggling.

 

I have the following data in an array called $alpha1 (approx 1000 entries)

OID=.1.3.6.1.4.1.14823.2.2.1.1.2.1.1.2.10.12.15.161, Type=OctetString, Value= D8 B0 53 C4 01 E5

OID=.1.3.6.1.4.1.14823.2.2.1.1.2.1.1.2.10.15.65.47, Type=OctetString, Value= 5C D0 6E F0 F9 2E

OID=.1.3.6.1.4.1.14823.2.2.1.1.2.1.1.2.10.36.240.52, Type=OctetString, Value= 84 B1 E4 6E 93 4F

OID=.1.3.6.1.4.1.14823.2.2.1.1.2.1.1.2.10.36.240.53, Type=OctetString, Value= 1C 6A 76 41 09 B9

OID=.1.3.6.1.4.1.14823.2.2.1.1.2.1.1.2.10.36.240.59, Type=OctetString, Value= 84 B1 E4 70 C7 D9

OID=.1.3.6.1.4.1.14823.2.2.1.1.2.1.1.2.10.36.240.60, Type=OctetString, Value= 6C 7E 67 41 27 C3

OID=.1.3.6.1.4.1.14823.2.2.1.1.2.1.1.2.10.36.240.67, Type=OctetString, Value= CE B9 5E 32 17 61

 

In a second array $alpha2, I have the following data (approx 1000 entries)

 

OID=.1.3.6.1.4.1.14823.2.2.1.1.2.1.1.3.10.12.15.161, Type=OctetString, Value=GA871

OID=.1.3.6.1.4.1.14823.2.2.1.1.2.1.1.3.10.15.65.47, Type=OctetString, Value=MC990

OID=.1.3.6.1.4.1.14823.2.2.1.1.2.1.1.3.10.36.240.52, Type=OctetString, Value=svc_rover@gstt.local

OID=.1.3.6.1.4.1.14823.2.2.1.1.2.1.1.3.10.36.240.53, Type=OctetString, Value=svc_rover@gstt.local

OID=.1.3.6.1.4.1.14823.2.2.1.1.2.1.1.3.10.36.240.59, Type=OctetString, Value=svc_rover@gstt.local

OID=.1.3.6.1.4.1.14823.2.2.1.1.2.1.1.3.10.36.240.60, Type=OctetString, Value=svc_rover@gstt.local

OID=.1.3.6.1.4.1.14823.2.2.1.1.2.1.1.3.10.36.240.67, Type=OctetString, Value=svc_rover@gstt.local

 

What I require is some php code to do the following, loop through all the entries in $alpha1 and if (where I have bold and underlined the text,  a match is found in $alpha2, then to extract the text  which I have bold and italics from both $alpha1 and $alpha2? 

I am using PHP 7.3.1

Thank you

Link to comment
Share on other sites

I guess I'll never know how and why they were created in such an unusable format.

You first task is to correct that shortcoming and restructure the two arrays. I suggest

$alpha1 = [
            "OID=.1.3.6.1.4.1.14823.2.2.1.1.2.1.1.2.10.12.15.161, Type=OctetString, Value= D8 B0 53 C4 01 E5",
            "OID=.1.3.6.1.4.1.14823.2.2.1.1.2.1.1.2.10.15.65.47, Type=OctetString, Value= 5C D0 6E F0 F9 2E",
            "OID=.1.3.6.1.4.1.14823.2.2.1.1.2.1.1.2.10.36.240.52, Type=OctetString, Value= 84 B1 E4 6E 93 4F",
            "OID=.1.3.6.1.4.1.14823.2.2.1.1.2.1.1.2.10.36.240.53, Type=OctetString, Value= 1C 6A 76 41 09 B9",
            "OID=.1.3.6.1.4.1.14823.2.2.1.1.2.1.1.2.10.36.240.59, Type=OctetString, Value= 84 B1 E4 70 C7 D9",
            "OID=.1.3.6.1.4.1.14823.2.2.1.1.2.1.1.2.10.36.240.60, Type=OctetString, Value= 6C 7E 67 41 27 C3",
            "OID=.1.3.6.1.4.1.14823.2.2.1.1.2.1.1.2.10.36.240.67, Type=OctetString, Value= CE B9 5E 32 17 61"
            ]; 

$alpha2 = [
            "OID=.1.3.6.1.4.1.14823.2.2.1.1.2.1.1.3.10.12.15.161, Type=OctetString, Value=GA871",
            "OID=.1.3.6.1.4.1.14823.2.2.1.1.2.1.1.3.10.15.65.47, Type=OctetString, Value=MC990",
            "OID=.1.3.6.1.4.1.14823.2.2.1.1.2.1.1.3.10.36.240.52, Type=OctetString, Value=svc_rover@gstt.local",
            "OID=.1.3.6.1.4.1.14823.2.2.1.1.2.1.1.3.10.36.240.53, Type=OctetString, Value=svc_rover@gstt.local",
            "OID=.1.3.6.1.4.1.14823.2.2.1.1.2.1.1.3.10.36.240.59, Type=OctetString, Value=svc_rover@gstt.local",
            "OID=.1.3.6.1.4.1.14823.2.2.1.1.2.1.1.3.10.36.240.60, Type=OctetString, Value=svc_rover@gstt.local",
            "OID=.1.3.6.1.4.1.14823.2.2.1.1.2.1.1.3.10.36.240.67, Type=OctetString, Value=svc_rover@gstt.local"
            ];
            


$alpha1 = array_map('convert', $alpha1);    // apply the calback function convert() to each element
$alpha2 = array_map('convert', $alpha2);

function convert($str)     // callback function
{
    $a = explode(', ', $str);
    $res = [];
    foreach ($a as $str2)  {
        $b = explode('=', $str2);
        if ($b[0] == 'OID')  {
            // we only want the last 4 elements
            $res[$b[0]] = join('.', array_slice(explode('.', $b[1]), -4));
        }
        else $res[$b[0]] = $b[1];
    }
    return $res ;
}

You now have two arrays in this format, which are much more easily processed...

$arr = array (
  0 => array (
    'OID' => '10.12.15.161',
    'Type' => 'OctetString',
    'Value' => ' D8 B0 53 C4 01 E5',
  ),
  1 => array (
    'OID' => '10.15.65.47',
    'Type' => 'OctetString',
    'Value' => ' 5C D0 6E F0 F9 2E',
  ),
  2 => array (
    'OID' => '10.36.240.52',
    'Type' => 'OctetString',
    'Value' => ' 84 B1 E4 6E 93 4F',
  ),
  3 => array (
    'OID' => '10.36.240.53',
    'Type' => 'OctetString',
    'Value' => ' 1C 6A 76 41 09 B9',
  ),
  4 => array (
    'OID' => '10.36.240.59',
    'Type' => 'OctetString',
    'Value' => ' 84 B1 E4 70 C7 D9',
  ),
  5 => array (
    'OID' => '10.36.240.60',
    'Type' => 'OctetString',
    'Value' => ' 6C 7E 67 41 27 C3',
  ),
  6 => array (
    'OID' => '10.36.240.67',
    'Type' => 'OctetString',
    'Value' => ' CE B9 5E 32 17 61',
  ),
);

That's the hard part done. Now it's just a matter of looping through one array and looking for matches in the other.

  • Great Answer 1
Link to comment
Share on other sites

Hi Barand, thank you for your prompt response.

 

I see you live in Cheshire. I use to live in Winsford, Cheshire. Where do you live?

 

When you say, That's the hard part done. Now it's just a matter of looping through one array and looking for matches in the other, can you please show me how to do this?

 

In regards to your question, the data was produced by SNMP queries against a master switch.

Link to comment
Share on other sites

To loop through an array, use foreach().

There are a couple of ways to skin this cat.

  • METHOD 1
    • Loop through alpaha1 getting the OID and Value from each element
    • For each element search alpha2 for the matching OID and get its value
  • METHOD 2
    • Extract array of values from each array with OID as the array keys

METHOD 1

$results = [];
// loop through first array getting OIDs and Values
foreach ($alpha1 as $arr)  {
    $results[$arr['OID']] = [ 'val1' => $arr['Value'], 'val2' => '' ];
    // search second array for same OID and get its value
    foreach ($alpha2 as $arr2)  {
        if ($arr2['OID'] == $arr['OID'])  {
            $results[$arr['OID']]['val2'] = $arr2['Value'];
            break;
        }
    }
}
// print the results
echo '<pre>';
printf("%-20s%-30s%-30s<br><br>", 'OID', 'Val 1', 'Val 2');
foreach ($results as $k => $v)  {
    printf("%-20s%-30s%-30s<br>", $k, $v['val1'], $v['val2']);
}
echo '</pre>';

METHOD 2

$values1 = array_column($alpha1, 'Value', 'OID');
$values2 = array_column($alpha2, 'Value', 'OID');

echo '<pre>';
printf("%-20s%-30s%-30s<br><br>", 'OID', 'Val 1', 'Val 2'); 
foreach ($values1 as $k => $v)  {
   printf("%-20s%-30s%-30s<br>", $k, $v, $values2[$k]); 
}
echo '</pre>';

Both methods output ...

OID                 Val 1                         Val 2                         

10.12.15.161         D8 B0 53 C4 01 E5            GA871                         
10.15.65.47          5C D0 6E F0 F9 2E            MC990                         
10.36.240.52         84 B1 E4 6E 93 4F            svc_rover@gstt.local          
10.36.240.53         1C 6A 76 41 09 B9            svc_rover@gstt.local          
10.36.240.59         84 B1 E4 70 C7 D9            svc_rover@gstt.local          
10.36.240.60         6C 7E 67 41 27 C3            svc_rover@gstt.local          
10.36.240.67         CE B9 5E 32 17 61            svc_rover@gstt.local  

 

Link to comment
Share on other sites

This does not work

 

$values1 = array_column($alpha1, 'Value', 'OID');
$values2 = array_column($alpha2, 'Value', 'OID');

$values3 = array_column($alpha3, 'Value', 'OID');
$values4 = array_column($alpha4, 'Value', 'OID');


foreach ($values1 as $k => $v)  {

echo "<tr>";
   echo "<TD>" . $k . "</td>";
   echo "<TD>" . $v . "</TD>";
   echo "<TD>" . $values2[$k] . "</td>"; 


foreach ($values3 as $z => $w)  {

   echo "<TD>" . $z . "</td>";
   echo "<TD>" . $w . "</TD>";
   echo "<TD>" . $values2[$z] . "</td>"; 

}

echo "</tr>";

Link to comment
Share on other sites

What error messages are you getting?

Do you have error reporting turned on?

[PS]...

image.png.a62f52ba567b64c9ef5223d7048a198c.png

$values 3 has an element where $z = "10.36.240.65". $values2 has no such element therefore it throws an undefined key error - which error reporting would have told you.

In your php.ini file, set

  • error_reporting = E_ALL
  • display_errors = on
  • display_startup_errors = on
Link to comment
Share on other sites

3 hours ago, fanta1066 said:

Ok I am giving up, its far to complicated as I am not a PHP coder. And this has taken up to much of my time. 

Sorry to see your valuable time was wasted by Barand providing you free professional consulting, custom built code and hand holding.  😣  

  • Like 2
Link to comment
Share on other sites

On 2/2/2024 at 8:04 AM, fanta1066 said:

Ok I am giving up, its far to complicated as I am not a PHP coder. And this has taken up to much of my time.

As I'm sure you know, the learning process can be frustrating, especially when things don't seem to click...and/or when rushed to find a solution. Feel free to come back if you decide to tackle the problem again.

To go along with Barand's suggestion, one option to show PHP errors is adding the following to the top of your script while debugging:

<?php
ini_set('error_reporting', E_ALL);
ini_set('display_errors', 1);
?>

 

On 1/31/2024 at 10:15 AM, fanta1066 said:

I am using PHP 7.3.1

Side note: the link below shows the PHP versions that are actively being supported.
https://www.php.net/supported-versions

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.