fanta1066 Posted January 31 Share Posted January 31 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 Quote Link to comment Share on other sites More sharing options...
ginerjm Posted January 31 Share Posted January 31 It appears to be a very simple problem. What have you tried so far? Show us so we can help you out. Or you just asking us to write a script for you? Quote Link to comment Share on other sites More sharing options...
fanta1066 Posted January 31 Author Share Posted January 31 Hi, I am a network engineer not a coder. Having said that, there are times when I need to write some basic code to get me particular data. I have looked here for a solution https://thispointer.com/check-if-array-contains-a-substring-in-php/ Though, I am not able to find a working solution. So can you help me plz? Quote Link to comment Share on other sites More sharing options...
Barand Posted January 31 Share Posted January 31 Are those arrays coming from database tables? Quote Link to comment Share on other sites More sharing options...
Barand Posted January 31 Share Posted January 31 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. 1 Quote Link to comment Share on other sites More sharing options...
fanta1066 Posted February 1 Author Share Posted February 1 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. Quote Link to comment Share on other sites More sharing options...
Barand Posted February 1 Share Posted February 1 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 Quote Link to comment Share on other sites More sharing options...
fanta1066 Posted February 1 Author Share Posted February 1 Hi Barand, I have tried both solutions you have kindly offered. However when I try each one, I only get the following output OID Val 1 Val 2 Quote Link to comment Share on other sites More sharing options...
fanta1066 Posted February 1 Author Share Posted February 1 Apologies, its now working. Thank you very much for all your help. May I ask you another question please? Quote Link to comment Share on other sites More sharing options...
Barand Posted February 1 Share Posted February 1 If it is not connected with this topic, start another one. Quote Link to comment Share on other sites More sharing options...
fanta1066 Posted February 2 Author Share Posted February 2 Yes I understand, but I have to present all the information in the form of a table. This is another problem. How can I take all that data and present in a table? Quote Link to comment Share on other sites More sharing options...
fanta1066 Posted February 2 Author Share Posted February 2 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>"; Quote Link to comment Share on other sites More sharing options...
Barand Posted February 2 Share Posted February 2 What error messages are you getting? Do you have error reporting turned on? [PS]... $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 Quote Link to comment Share on other sites More sharing options...
fanta1066 Posted February 2 Author Share Posted February 2 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. Quote Link to comment Share on other sites More sharing options...
gizmola Posted February 2 Share Posted February 2 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. 😣 2 Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted February 5 Share Posted February 5 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 Quote Link to comment Share on other sites More sharing options...
Barand Posted February 5 Share Posted February 5 A little exercise in logic... If you have startup errors, the code is not executed. If the code is not executed, how can you execute "display_startup_errors"? It has to be pre-set in the php.ini file. Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted February 5 Share Posted February 5 Thanks for the clarification! I removed the line from my post. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.