Omzy Posted August 14, 2009 Share Posted August 14, 2009 I have a checkbox array called 'selection' ($_POST['selection']) If I want to check for a value against the values held in the array, is it best to use: 1) if (in_array($value, $_POST['selection'])) or: 2) if (in_array($value, array_values($_POST['selection']))) Form my testing both of these methods work. Also I found that method 1 does not match against the array INDEX/KEY - is this the correct behaviour? Quote Link to comment https://forums.phpfreaks.com/topic/170319-in_array/ Share on other sites More sharing options...
smerny Posted August 14, 2009 Share Posted August 14, 2009 if ($value == $_POST['selection']) this seems like what you need... $_POST['selection'] is one element of the array, or a value... not an array itself Quote Link to comment https://forums.phpfreaks.com/topic/170319-in_array/#findComment-898454 Share on other sites More sharing options...
Omzy Posted August 14, 2009 Author Share Posted August 14, 2009 No, because it's checkboxes, all checkboxes have to have the same NAME, therefore I've declared it as an array - <input type="checkbox" name="selection[]" value=".......... Quote Link to comment https://forums.phpfreaks.com/topic/170319-in_array/#findComment-898457 Share on other sites More sharing options...
wildteen88 Posted August 14, 2009 Share Posted August 14, 2009 I have a checkbox array called 'selection' ($_POST['selection']) If I want to check for a value against the values held in the array, is it best to use: 1) if (in_array($value, $_POST['selection'])) or: 2) if (in_array($value, array_values($_POST['selection']))) Form my testing both of these methods work. You can use either method. Both will work just fine. However the 2nd method seems unnecessary. Quote Link to comment https://forums.phpfreaks.com/topic/170319-in_array/#findComment-898477 Share on other sites More sharing options...
Omzy Posted August 14, 2009 Author Share Posted August 14, 2009 Cool. I was just after the most efficient method, i.e. I only want to search on the array values and not the keys. Quote Link to comment https://forums.phpfreaks.com/topic/170319-in_array/#findComment-898479 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.