parapro Posted November 26, 2011 Share Posted November 26, 2011 Can someone please help me figure this out: I have a normal text file with 3 lines in it, every line contain one date i.e 12112011 (12.11-2011). I put the content of that file in an array, and want to compair the dates in the text file against the date generated from the loop. Problem is, i can only get a match for the last date listed in the text file no matter what.. If i have 4 dates in the file, it only style the last date. Thanks! --- CODE SNIPPET --- $day_num = 1; $days_in_month = 30; $month = 11; $year = 2011; //Access the file containing already taken dates and load them in an array: $date_array = file("test.txt"); //count up the days, untill we've done all of them in the month and check them against the dates in the text file: while ($day_num <= $days_in_month) { // Set the variable $date to be checked against the content of the array: $date = $day_num . $month . $year; if (in_array($date, $date_array)) { echo "<td class='color_green'>$day_num</td>"; //print_r($date_array); } else { echo "<td>$day_num</td><br />"; } $day_num++; } Quote Link to comment https://forums.phpfreaks.com/topic/251860-if-in_array-only-check-against-the-last-value-in-my-array/ Share on other sites More sharing options...
kicken Posted November 26, 2011 Share Posted November 26, 2011 file() leaves the new lines on the end of each line by default. Each entry in your array then is going to have a newline sequence following it which is why your matches do not work. $date_array = file('test.txt', FILE_IGNORE_NEW_LINES); Quote Link to comment https://forums.phpfreaks.com/topic/251860-if-in_array-only-check-against-the-last-value-in-my-array/#findComment-1291417 Share on other sites More sharing options...
parapro Posted November 26, 2011 Author Share Posted November 26, 2011 Thank you for the quick reply. Tested the fix and it works. Thank you so much for your assistance, been busy for 2 days (lol) trying to figure out what was wrong Quote Link to comment https://forums.phpfreaks.com/topic/251860-if-in_array-only-check-against-the-last-value-in-my-array/#findComment-1291428 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.