turpentyne Posted February 20, 2012 Share Posted February 20, 2012 This is probably a simple one, but I'm not experienced with arrays. I have a form with looped dropdowns for items from a database. On submit it goes to a second page. Before I run any script I want to make sure the array created from the dropdowns contains anything greater than 0... I thought this would do it: if (isset($_POST['participantqty']) && ($_POST['participantqty']) > 0){ But it does nothing. I also tried: if (isset($_POST['participantqty[]']) && ($_POST['participantqty[]']) > 0){ Quote Link to comment https://forums.phpfreaks.com/topic/257383-validate-an-array-has-any-0/ Share on other sites More sharing options...
Psycho Posted February 20, 2012 Share Posted February 20, 2012 max() This will return the highest value in the array. But, you are referring to the field incorrectly if (isset($_POST['participantqty']) && max($_POST['participantqty']) > 0){ You would likely need to do some additional validations based upon what you expect the values to be. If you don't want the '0' values int eh array you might consider running it through array_filter() first. Quote Link to comment https://forums.phpfreaks.com/topic/257383-validate-an-array-has-any-0/#findComment-1319225 Share on other sites More sharing options...
Rifts Posted February 20, 2012 Share Posted February 20, 2012 or just use $count = count(arrayname); if($count > 0) { //do stuff } Quote Link to comment https://forums.phpfreaks.com/topic/257383-validate-an-array-has-any-0/#findComment-1319239 Share on other sites More sharing options...
Pikachu2000 Posted February 20, 2012 Share Posted February 20, 2012 Count doesn't tell you if any of the values in an array are > 0. Quote Link to comment https://forums.phpfreaks.com/topic/257383-validate-an-array-has-any-0/#findComment-1319244 Share on other sites More sharing options...
Rifts Posted February 20, 2012 Share Posted February 20, 2012 values? I thought he just wanted to check if the array was empty or not? Quote Link to comment https://forums.phpfreaks.com/topic/257383-validate-an-array-has-any-0/#findComment-1319249 Share on other sites More sharing options...
Pikachu2000 Posted February 20, 2012 Share Posted February 20, 2012 This is probably a simple one, but I'm not experienced with arrays. I have a form with looped dropdowns for items from a database. On submit it goes to a second page. Before I run any script I want to make sure the array created from the dropdowns contains anything greater than 0... I thought this would do it: if (isset($_POST['participantqty']) && ($_POST['participantqty']) > 0){ But it does nothing. I also tried: if (isset($_POST['participantqty[]']) && ($_POST['participantqty[]']) > 0){ Quote Link to comment https://forums.phpfreaks.com/topic/257383-validate-an-array-has-any-0/#findComment-1319250 Share on other sites More sharing options...
Rifts Posted February 20, 2012 Share Posted February 20, 2012 Touché, his wording is a bit confusing though... Quote Link to comment https://forums.phpfreaks.com/topic/257383-validate-an-array-has-any-0/#findComment-1319253 Share on other sites More sharing options...
turpentyne Posted February 20, 2012 Author Share Posted February 20, 2012 hehe! Sorry. I have a bad habit of posting a quick message when I'm running late, and headed out the door. My clarity probably suffers. But the first response seems to have worked perfectly for what I needed. Quote Link to comment https://forums.phpfreaks.com/topic/257383-validate-an-array-has-any-0/#findComment-1319320 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.