mrjgx Posted January 4, 2008 Share Posted January 4, 2008 help guys. i have a problem here. whenever i tried to access the array list, this problem occurs: Warning: Invalid argument supplied for foreach() in f:\mddd\www\altec\screen.php on line 120 //to query network client's running software $detection = @ snmpwalk($ip_add, "public", ".1.3.6.1.2.1.25.4.2.1.2", 5); foreach ($detection as $val) { if ($val == "firefox.exe") { echo "firefox is opened"; } elseif ($val == "IEXPLORE.EXE") { echo "Internet Explorer is opened"; } } is there something wrong? it seems that i couldnt match the array's character using the if else condition.. thx in advance Quote Link to comment https://forums.phpfreaks.com/topic/84400-solved-accessing-arrays-loop/ Share on other sites More sharing options...
papaface Posted January 4, 2008 Share Posted January 4, 2008 $detection isnt an array it is a function. If you want it to be an array do: $detection = array($ip_add, "public", ".1.3.6.1.2.1.25.4.2.1.2", 5); Quote Link to comment https://forums.phpfreaks.com/topic/84400-solved-accessing-arrays-loop/#findComment-429883 Share on other sites More sharing options...
mrjgx Posted January 4, 2008 Author Share Posted January 4, 2008 thanks for your reply. i cannot make it into an array because it is a function that is needed. $detection = array($ip_add, "public", ".1.3.6.1.2.1.25.4.2.1.2", 5); i need to use the function snmpwalk to get the list of running software names. i can echo $val<BR> and it will print out all the running names like this: "System Idle Process" "System" "MDM.EXE" "MioNetManager.exe" "Ymsgr_tray.exe" "avgcc.exe" "ati2evxx.exe" "explorer.exe" "smss.exe" "atiptaxx.exe" "sm56hlpr.exe" "csrss.exe" "googletalk.exe" "winlogon.exe" "services.exe" "lsass.exe" "SOUNDMAN.EXE" "ati2evxx.exe" "svchost.exe" "svchost.exe" "DATALA~1.EXE" "svchost.exe" "TRAYAP~1.EXE" "Apache.exe" "SearchProtection.exe" "daemon.exe" "svchost.exe" "Dreamweaver.exe" "PhiBtn.exe" "svchost.exe" "Tray900.exe" "AppleMobileDeviceService.exe" "ctfmon.exe" "spoolsv.exe" "avgamsvr.exe" "Apache.exe" "WLANUTL.exe" "avgupsvc.exe" "avgemc.exe" "DkService.exe" "SERVIC~1.EXE" "ijplmsvc.exe" "sqlservr.exe" "MioNet.exe" "mysqld.exe" "snmp.exe" "svchost.exe" "firefox.exe" "wdfmgr.exe" "IEXPLORE.EXE" "alg.exe" "wscntfy.exe" "svchost.exe" i need to know how to match the list of names using the loop? is there a way to do that?? thanks Quote Link to comment https://forums.phpfreaks.com/topic/84400-solved-accessing-arrays-loop/#findComment-429889 Share on other sites More sharing options...
kenrbnsn Posted January 4, 2008 Share Posted January 4, 2008 The function snmpwalk() returns an array. Since you prefixed the function call with the "@", you're suppressing any error message that might be coming from the function. Remove the "@" and see if an error is returned. Ken Quote Link to comment https://forums.phpfreaks.com/topic/84400-solved-accessing-arrays-loop/#findComment-429891 Share on other sites More sharing options...
redarrow Posted January 4, 2008 Share Posted January 4, 2008 there no point in that code as the .exe program is there even if you dont execute it...... unless that what the third parameter for.... please tell me.. Quote Link to comment https://forums.phpfreaks.com/topic/84400-solved-accessing-arrays-loop/#findComment-429894 Share on other sites More sharing options...
mrjgx Posted January 4, 2008 Author Share Posted January 4, 2008 ok now i finally got no errors. the code runs well, only thing that i need to know is how to match the arrays from snmpwalk function into the if-else condition. by using this code: foreach ($detection as $val) { if ($val == "firefox.exe") { echo "firefox is opened"; } elseif ($val == "IEXPLORE.EXE") { echo "Internet Explorer is opened"; } } but the echo doesnt appear, meaning that it cannot match the array names. is there a way to access the arrays 1 by 1 and match the strings using if-else condition? thank you. Quote Link to comment https://forums.phpfreaks.com/topic/84400-solved-accessing-arrays-loop/#findComment-429895 Share on other sites More sharing options...
redarrow Posted January 4, 2008 Share Posted January 4, 2008 you can try that...... <?php $val= snmpwalk($ip_add, "public", ".1.3.6.1.2.1.25.4.2.1.2", 5); if (in_array("firefox.exe",$val)) { echo "firefox is opened"; } elseif (in_array("IEXPLORE.EXE",$val)) { echo "Internet Explorer is opened"; } } ?> you sure the elseif is even valid say you use both i do... Quote Link to comment https://forums.phpfreaks.com/topic/84400-solved-accessing-arrays-loop/#findComment-429901 Share on other sites More sharing options...
mrjgx Posted January 4, 2008 Author Share Posted January 4, 2008 Thanks for the reply..but i get this error message instead. Warning: in_array(): Wrong datatype for second argument in f:\mddd\www\altec\screen.php on line 120 Warning: in_array(): Wrong datatype for second argument in f:\mddd\www\altec\screen.php on line .... Warning: in_array(): Wrong datatype for second argument in f:\mddd\www\altec\screen.php on line .... Warning: in_array(): Wrong datatype for second argument in f:\mddd\www\altec\screen.php on line ...n is there a way to rectify that? thanks Quote Link to comment https://forums.phpfreaks.com/topic/84400-solved-accessing-arrays-loop/#findComment-429905 Share on other sites More sharing options...
redarrow Posted January 4, 2008 Share Posted January 4, 2008 no function snmpwalk() installed on my server sorry Quote Link to comment https://forums.phpfreaks.com/topic/84400-solved-accessing-arrays-loop/#findComment-429910 Share on other sites More sharing options...
mrjgx Posted January 4, 2008 Author Share Posted January 4, 2008 can you explain what is the use of $x=$re[]; ? anyway i got error message : Fatal error: Cannot use [] for reading in f:\mddd\www\altec\screen.php on line 120 huhu 'm lost ??? Quote Link to comment https://forums.phpfreaks.com/topic/84400-solved-accessing-arrays-loop/#findComment-429917 Share on other sites More sharing options...
redarrow Posted January 4, 2008 Share Posted January 4, 2008 take no notice off my code example m8 i have not got this function installed sorry have to wait for a diffrent user to help you........... Quote Link to comment https://forums.phpfreaks.com/topic/84400-solved-accessing-arrays-loop/#findComment-429942 Share on other sites More sharing options...
mrjgx Posted January 4, 2008 Author Share Posted January 4, 2008 thanks anyway for your help so is there someone out there can help me then? :'( Quote Link to comment https://forums.phpfreaks.com/topic/84400-solved-accessing-arrays-loop/#findComment-429947 Share on other sites More sharing options...
teng84 Posted January 4, 2008 Share Posted January 4, 2008 i believe this is none sense can you explain what is the use of $x=$re[]; ? if you dont get an error out of that you will get empty Quote Link to comment https://forums.phpfreaks.com/topic/84400-solved-accessing-arrays-loop/#findComment-429951 Share on other sites More sharing options...
hitman6003 Posted January 4, 2008 Share Posted January 4, 2008 are you sure that your snmpwalk function is returning the results you are expecting? use print_r to view the contents of the array: echo '<pre>' . print_r($detection, true) . '</pre>'; if it is an array, then use some normalization on the data returned to make sure it's matching your string: if (in_array("firefox.exe",strtolower(trim($val)))) { echo "firefox is opened"; } else if (in_array("iexplore.exe",strtolower(trim($val)))) { echo "Internet Explorer is opened"; } Quote Link to comment https://forums.phpfreaks.com/topic/84400-solved-accessing-arrays-loop/#findComment-429954 Share on other sites More sharing options...
mrjgx Posted January 4, 2008 Author Share Posted January 4, 2008 hey thanks..the print_r function will displays this list: Array ( [0] => "System Idle Process" [1] => "System" [2] => "MDM.EXE" [3] => "MioNetManager.exe" [4] => "Ymsgr_tray.exe" [5] => "avgcc.exe" [6] => "ati2evxx.exe" [7] => "explorer.exe" [8] => "smss.exe" [9] => "atiptaxx.exe" [10] => "sm56hlpr.exe" [11] => "csrss.exe" [12] => "googletalk.exe" [13] => "winlogon.exe" [14] => "services.exe" [15] => "lsass.exe" [16] => "SOUNDMAN.EXE" [17] => "ati2evxx.exe" [18] => "svchost.exe" [19] => "svchost.exe" [20] => "DATALA~1.EXE" [21] => "svchost.exe" [22] => "TRAYAP~1.EXE" [23] => "Apache.exe" [24] => "SearchProtection.exe" [25] => "daemon.exe" [26] => "svchost.exe" [27] => "Dreamweaver.exe" [28] => "PhiBtn.exe" [29] => "svchost.exe" [30] => "Tray900.exe" [31] => "AppleMobileDeviceService.exe" [32] => "ctfmon.exe" [33] => "spoolsv.exe" [34] => "avgamsvr.exe" [35] => "Apache.exe" [36] => "WLANUTL.exe" [37] => "avgupsvc.exe" [38] => "avgemc.exe" [39] => "DkService.exe" [40] => "SERVIC~1.EXE" [41] => "ijplmsvc.exe" [42] => "sqlservr.exe" [43] => "MioNet.exe" [44] => "mysqld.exe" [45] => "snmp.exe" [46] => "svchost.exe" [47] => "firefox.exe" [48] => "wdfmgr.exe" [49] => "IEXPLORE.EXE" [50] => "alg.exe" [51] => "wscntfy.exe" [52] => "svchost.exe" ) but the normalization on the data returned this error message: Warning: in_array(): Wrong datatype for second argument in f:\mddd\www\altec\screen.php on line 124 i need to know how to excerpt the array which have firefox or iexplorer words in it..is there a way? thx guys Quote Link to comment https://forums.phpfreaks.com/topic/84400-solved-accessing-arrays-loop/#findComment-429988 Share on other sites More sharing options...
kenrbnsn Posted January 4, 2008 Share Posted January 4, 2008 Try this: <?php $val= snmpwalk($ip_add, "public", ".1.3.6.1.2.1.25.4.2.1.2", 5); foreach ($val as $exe) switch ($val) { case 'IEXPLORE.EXE': echo "Internet Explorer is opened"; break; case 'firefox.exe': echo "firefox is opened"; break; } ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/84400-solved-accessing-arrays-loop/#findComment-430015 Share on other sites More sharing options...
mrjgx Posted January 4, 2008 Author Share Posted January 4, 2008 Thanks kenrbnsn..your switch code is kind same with my original switch code that i made.. finally i found the solutions to this problem...i just need to put the " ". this is the working code: foreach ($val as $exe) switch ($exe) { case '"IEXPLORE.EXE"': echo "Internet Explorer is opened<BR>"; break; case '"firefox.exe"': echo "firefox is opened<BR>"; break; } voila..all the problems solved..i can access each items in the array list... MILLION THANKS to all helpers. i'm so happy Quote Link to comment https://forums.phpfreaks.com/topic/84400-solved-accessing-arrays-loop/#findComment-430063 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.