jbond Posted May 17, 2010 Share Posted May 17, 2010 Hello everyone, I am trying to use the in_array function to determine whether a value is present or not. The array is passed as an argument to the function (see below) function voeg_gsm_bij($gsmnummer,$array_of_num) { if(in_array($gsmnummer,$array_of_num)){ echo "Nummer bestaat reeds"; } else { echo "Nummer niet gevonden in bestand"; } } Although $gsmnummer and $array_of_num are known inside the function, the in_array doesn't pick up the var $gsmnummer in the $array_of_num. When I run this manually with following values, the in_array routine performs correctly $array_of_num=array("0476903056","0488990044"); if(in_array("0476903056",$array_of_num)) I am obviously missing something somewhere, but can't figure out what I am doing wrong. Specifying an array inside the function runs correctly, passing on an array in the function doesn't work ??? Can somebody please help?? Thanks + best regards Link to comment https://forums.phpfreaks.com/topic/202012-using-the-in_array-function/ Share on other sites More sharing options...
Zyx Posted May 17, 2010 Share Posted May 17, 2010 The function works correctly. The most probable reason is that you misunderstand how it actually works. Check this out: $array_of_num=array("0476903056","0488990044"); voeg_gsm_bij("0476903056",$array_of_num); As expected, I get: Nummer bestaat reeds Link to comment https://forums.phpfreaks.com/topic/202012-using-the-in_array-function/#findComment-1059363 Share on other sites More sharing options...
jbond Posted May 17, 2010 Author Share Posted May 17, 2010 Hi Zyx, thank you for your reply. No, I think I do understand how it works and if I do something similar to what you have coded, it works. The problem is that I want to work with variables that are passed on and that need to be examined by the in_array function. Taking my previous example : function voeg_gsm_bij($gsmnummer,$array_of_num) { if(in_array($gsmnummer,$array_of_num)){ echo "Nummer bestaat reeds"; } else { echo "Nummer niet gevonden in bestand"; } } The function voeg_gsm_bij gets two parameters passed from another routine, i.e. the $gsmnummer (which is the number to be checked) and the $array_of_num which is in fact an array filled with numbers. I know that $array_of_num contains data because when I loop through it, it lists them on the screen correctly. So, in this array, the $gsmnummer exists, but the in_array reports it as "not found". So I am wondering whether using the function as I am trying to do yields any results. It seems to me as if the $array_of_num and the $gsmnummer are empty when using in_array($var1,$array1) ?? Link to comment https://forums.phpfreaks.com/topic/202012-using-the-in_array-function/#findComment-1059367 Share on other sites More sharing options...
jbond Posted May 17, 2010 Author Share Posted May 17, 2010 Figured it out.. Stupid mistake. When reading in the text file, forgot that there were two hidden characters behind every line (cr/lf), so instead of length of 10, length of string was 12. Therefor the comparison never worked. I have now changed it and the in_array function works just fine as expected B rgds Link to comment https://forums.phpfreaks.com/topic/202012-using-the-in_array-function/#findComment-1059444 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.