r00ttap Posted June 18, 2009 Share Posted June 18, 2009 How would I go about checking if a variable equals anything within an array. For instances: $array = ('2009-06-01', '2009-06-02', '2009-06-20', '2006-06-25'); $today = "2009-06-20"; if($today = $array) { echo "Today was found within the array"; } I know that won't work but it's what I'm looking to accomplish. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/162798-if-variable-anything-within-an-array/ Share on other sites More sharing options...
PFMaBiSmAd Posted June 18, 2009 Share Posted June 18, 2009 in_array Quote Link to comment https://forums.phpfreaks.com/topic/162798-if-variable-anything-within-an-array/#findComment-859061 Share on other sites More sharing options...
r00ttap Posted June 18, 2009 Author Share Posted June 18, 2009 Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/162798-if-variable-anything-within-an-array/#findComment-859062 Share on other sites More sharing options...
RussellReal Posted June 18, 2009 Share Posted June 18, 2009 wow.. (..,..,..,..) sets an array? Quote Link to comment https://forums.phpfreaks.com/topic/162798-if-variable-anything-within-an-array/#findComment-859063 Share on other sites More sharing options...
PFMaBiSmAd Posted June 18, 2009 Share Posted June 18, 2009 No (try it if you want), the first post was just pseudo code to illustrate what he wanted. Quote Link to comment https://forums.phpfreaks.com/topic/162798-if-variable-anything-within-an-array/#findComment-859082 Share on other sites More sharing options...
RussellReal Posted June 18, 2009 Share Posted June 18, 2009 oh, lmao I was like WOW WHY DIDN'T I KNOW THAT! Quote Link to comment https://forums.phpfreaks.com/topic/162798-if-variable-anything-within-an-array/#findComment-859124 Share on other sites More sharing options...
r00ttap Posted June 18, 2009 Author Share Posted June 18, 2009 Actually I'm going to ask another question that relates to this one. How does one set a while loop results as an array? $dateSelect = "SELECT dates FROM dates_table"; cancelDates = array(); while($row=mysql_fetch_assoc($dateSelect)) I basically want the dates to be set as an array. Quote Link to comment https://forums.phpfreaks.com/topic/162798-if-variable-anything-within-an-array/#findComment-859135 Share on other sites More sharing options...
akitchin Posted June 18, 2009 Share Posted June 18, 2009 are the dates being stored as a comma-delimited list, or are they listed individually in the dates_table? Quote Link to comment https://forums.phpfreaks.com/topic/162798-if-variable-anything-within-an-array/#findComment-859137 Share on other sites More sharing options...
r00ttap Posted June 18, 2009 Author Share Posted June 18, 2009 Individually, linked by id. Quote Link to comment https://forums.phpfreaks.com/topic/162798-if-variable-anything-within-an-array/#findComment-859143 Share on other sites More sharing options...
RussellReal Posted June 18, 2009 Share Posted June 18, 2009 $dates = array(); while ($r = mysql_fetch_assoc($dateSelect) ) { $dates[] = $r['dates']; } Quote Link to comment https://forums.phpfreaks.com/topic/162798-if-variable-anything-within-an-array/#findComment-859145 Share on other sites More sharing options...
r00ttap Posted June 18, 2009 Author Share Posted June 18, 2009 Okay, so if I wanted to check if a date was in that array I would use: if(in_array("2009-06-19", $dates)) { $checked = "checked"; } echo $checked; Why am I receiving this: Warning: in_array() [function.in-array]: Wrong datatype for second argument Quote Link to comment https://forums.phpfreaks.com/topic/162798-if-variable-anything-within-an-array/#findComment-859153 Share on other sites More sharing options...
PFMaBiSmAd Posted June 18, 2009 Share Posted June 18, 2009 The error means that $dates was not an array at the time that statement was executed. Did you verify what was in $dates at that point in the code? Quote Link to comment https://forums.phpfreaks.com/topic/162798-if-variable-anything-within-an-array/#findComment-859156 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.