k0z Posted May 20, 2010 Share Posted May 20, 2010 If I have an array of data, like: $times = array(1, 2, 3, 5, 6, 7); How can I determine that the number 4 is missing? The $times array will only ever hold data numbering from 1 to 7. These numbers represent the day of the week (1 is Monday, 7 is Sunday). So, for example, $times might be: $times = array(2, 6); In the above case, I would need to determine that the numbers 1, 3, 4, 5, and 7 were missing. Ideally, the missing numbers would be entered into another array. Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/202327-array-check/ Share on other sites More sharing options...
PFMaBiSmAd Posted May 20, 2010 Share Posted May 20, 2010 Make an reference array that contains all the numbers and use array_diff to find out which ones are missing in the target array - http://us.php.net/array_diff Quote Link to comment https://forums.phpfreaks.com/topic/202327-array-check/#findComment-1060891 Share on other sites More sharing options...
mrqpro Posted May 20, 2010 Share Posted May 20, 2010 You can use in_array function http://php.net/manual/en/function.in-array.php Ex: $times = array(1, 2, 3, 5, 6, 7); if (!in_array(4, $times)) { // do something } Quote Link to comment https://forums.phpfreaks.com/topic/202327-array-check/#findComment-1060944 Share on other sites More sharing options...
anups Posted May 20, 2010 Share Posted May 20, 2010 $days = array(1, 2, 3, 4, 5, 6, 7); $times = array(2,6); $missing = array_diff($days,$times); echo "<pre>"; print_r($missing); echo "</pre>"; Quote Link to comment https://forums.phpfreaks.com/topic/202327-array-check/#findComment-1060961 Share on other sites More sharing options...
The Little Guy Posted May 20, 2010 Share Posted May 20, 2010 $times = array(2, 6); $days = range(1, 7); $missing = array_diff($days, $times); print_r($missing); Quote Link to comment https://forums.phpfreaks.com/topic/202327-array-check/#findComment-1060965 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.