Jump to content

IF in_array only check against the last value in my array ..


parapro

Recommended Posts

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++;

}

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);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.