Jahren Posted December 3, 2008 Share Posted December 3, 2008 I have this little glimpse of code that wont work function valider_journees($nb_dispos) { $valide = false; $date = array(); //I have to start at 1. its been tested already for($i = 1; $i <= $nb_dispos; ++$i) { $valide = $valide && !in_array($_POST['date' . $i], $date); $date .= $_POST['date' . $i]; } return $valide; } when I echo the values : echo $_POST['date' . $i] . ' ' . !in_array($_POST['date' . $i], $date); I get this error : 27/01/2009 1 Warning: in_array() [function.in-array]: Wrong datatype for second argument in C:\Program Files\utilities\EasyPHP 2.0b1\www\gesti\ajouter_benevole.php on line 413 28/01/2009 I've tested types with gettype() and they all give me strings A little help ? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/135428-solved-in_array-problem-s/ Share on other sites More sharing options...
flyhoney Posted December 3, 2008 Share Posted December 3, 2008 I'm not sure why you are getting the error, as $date is explicitly cast as an array. Also, wont $valide be false, no matter what, since you are setting it to false at the beginning and always doing an &&? Quote Link to comment https://forums.phpfreaks.com/topic/135428-solved-in_array-problem-s/#findComment-705448 Share on other sites More sharing options...
flyhoney Posted December 3, 2008 Share Posted December 3, 2008 Oh I see You set $date as an array at the beginning, but then you use a string concat operator later '.=' Quote Link to comment https://forums.phpfreaks.com/topic/135428-solved-in_array-problem-s/#findComment-705449 Share on other sites More sharing options...
flyhoney Posted December 3, 2008 Share Posted December 3, 2008 Try this: <?php function valider_journees($nb_dispos) { $valide = false; $date = array(); //I have to start at 1. its been tested already for($i = 1; $i <= $nb_dispos; ++$i) { $valide = $valide && !in_array($_POST['date' . $i], $date); $date[] = $_POST['date' . $i]; } return $valide; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/135428-solved-in_array-problem-s/#findComment-705450 Share on other sites More sharing options...
Jahren Posted December 3, 2008 Author Share Posted December 3, 2008 there's a chance that $nb_dispos is < 1 so it could return crap and I really don't understand why it gives me this error. It's why i'm seeking knowledge here ehe ^^ Quote Link to comment https://forums.phpfreaks.com/topic/135428-solved-in_array-problem-s/#findComment-705451 Share on other sites More sharing options...
premiso Posted December 3, 2008 Share Posted December 3, 2008 for($i = 1; $i <= $nb_dispos; ++$i) { $valide = $valide && !in_array($_POST['date' . $i], $date); $date .= $_POST['date' . $i]; } You are setting $date to a string when it should be an array. Thus the error. $strDate = ""; for($i = 1; $i <= $nb_dispos; ++$i) { $valide = $valide && !in_array($_POST['date' . $i], $date); $strDate .= $_POST['date' . $i]; } Try that. This assumes you want it in a string, if you want it in an array, use flyhoney's version. Quote Link to comment https://forums.phpfreaks.com/topic/135428-solved-in_array-problem-s/#findComment-705452 Share on other sites More sharing options...
Jahren Posted December 3, 2008 Author Share Posted December 3, 2008 flyhoney's guess was right. However something is still wrong as it returns false when it shouldn't. i'll check it out. thanks for your inputs i'll give feedback in a sec Quote Link to comment https://forums.phpfreaks.com/topic/135428-solved-in_array-problem-s/#findComment-705456 Share on other sites More sharing options...
flyhoney Posted December 3, 2008 Share Posted December 3, 2008 Like I said, I think it will return false always. Quote Link to comment https://forums.phpfreaks.com/topic/135428-solved-in_array-problem-s/#findComment-705458 Share on other sites More sharing options...
Jahren Posted December 3, 2008 Author Share Posted December 3, 2008 ahaha, yep that's right. edit : its been 14 hours straigtht every day of coding including weekends for a full month. I have a week to go only as a deadline to have everything working top notch. i'm tired as hell. thanks everyone for your support. Quote Link to comment https://forums.phpfreaks.com/topic/135428-solved-in_array-problem-s/#findComment-705459 Share on other sites More sharing options...
premiso Posted December 3, 2008 Share Posted December 3, 2008 Well for starters your for is weird, it starts at 1 and adds 1 to it before anything happens, thus you start at 2. //I have to start at 1. its been tested already for($i = 1; $i <= $nb_dispos; $i++) { $valide = $valide && !in_array($_POST['date' . $i], $date); $date[] = $_POST['date' . $i]; } That would be better, if you do not have a date0 and only have a date1...etc. Also this part: $valide = $valide && !in_array($_POST['date' . $i], $date); Is weird as $valide will only amount to the last date. So if the last date is in the array, than it will always return false. And if $valide, is false than it will always return false, cause $valide can never be set to true. That statement in itself is flawed. Hope that helps to get you on the right track. I would be more than happy to help more, I just need to know what your goal is for this function. What does it ultimately need to do? Quote Link to comment https://forums.phpfreaks.com/topic/135428-solved-in_array-problem-s/#findComment-705464 Share on other sites More sharing options...
Jahren Posted December 3, 2008 Author Share Posted December 3, 2008 indeed. It was flawed. I changed the initial value from false to $valide = $nb_dispos > 0; so if I have no element to check, it returns false. and there is no date0, this is why I start at 1. now it works like a charm Edit : This function is used to verify that there are no double inputs in all the form. I do not know how many entries I have to check before it is sent which is why I have to use that weird setup. (PHP generating Javascript Generating HTML) Quote Link to comment https://forums.phpfreaks.com/topic/135428-solved-in_array-problem-s/#findComment-705467 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.