php - Last item in foreach loop is the array it is traversing -
i have array of weeks, next weeks , week after's dates holding date, opening time , closing time. reason, when foreach loop reaches final date, outputs array of final week instead of final date. here start of array:
$dates = array ( [0] => array ( [0] => array ( [0] => "mon 23rd june" [1] => "9:00am" [2] => "7:00pm" ) [1] => array ( [0] => "tue 24th june" [1] => "9:00am" [2] => "7:00pm" ) ... [1] => array ( [0] => array ( [0] => "mon 30th june" [1] => "9:00am" [2] => "7:00pm" ) [1] => array ( [0] => "tue 1st july" [1] => "9:00am" [2] => "7:00pm" ) ... there no issue array, have printed out. here foreach loop (it nested foreach ($week $day) error occurs):
foreach($dates $week) { if($i == 2) { $html .= '<table cellpadding="5" cellspacing="2" border="0" class="kc_ot_openingtable last">'; } else { $html .= '<table cellpadding="5" cellspacing="2" border="0" class="kc_ot_openingtable">'; } $html.= '<tr class="kc_ot_weekcommence"> <td colspan="3">week commencing '.$week[0][0].'</td> </tr> <tr class="kc_ot_openingtabletitle"> <td class="day">day</td> <td class="open">open</td> <td class="closed">closed</td> </tr>'; foreach($week $day) { $html .= '<tr> <td>'.$day[0].'</td> <td class="open">'.$day[1].'</td> <td class="closed">'.$day[2].'</td> </tr> <tr>'; } $html .= '</table>'; ++$i; } can spot going on?
edit
i have found out $dates fine, problem occurs when foreach($dates $week) loop runs on last week.
re edit
here function comes from. please don't judge, inherited site :p
function getopeninghours() { date_default_timezone_set('europe/london'); $dates = array( array( array( date("d js f", strtotime("monday week")), "9:00am", "7:00pm" ), array( date("d js f", strtotime("tuesday week")), "9:00am", "7:00pm" ), array( date("d js f", strtotime("wednesday week")), "9:00am", "7:00pm" ), array( date("d js f", strtotime("thursday week")), "9:00am", "7:00pm" ), array( date("d js f", strtotime("friday week")), "9:00am", "7:00pm" ), array( date("d js f", strtotime("saturday week")), "9:00am", "7:00pm" ), array( date("d js f", strtotime("sunday week")), "9:00am", "7:00pm" ), ), array( array( date("d js f", strtotime("monday next week")), "9:00am", "7:00pm" ), array( date("d js f", strtotime("tuesday next week")), "9:00am", "7:00pm" ), array( date("d js f", strtotime("wednesday next week")), "9:00am", "7:00pm" ), array( date("d js f", strtotime("thursday next week")), "9:00am", "7:00pm" ), array( date("d js f", strtotime("friday next week")), "9:00am", "7:00pm" ), array( date("d js f", strtotime("saturday next week")), "9:00am", "7:00pm" ), array( date("d js f", strtotime("sunday next week")), "9:00am", "7:00pm" ), ), array( array( date("d js f", strtotime("monday next week", strtotime("monday next week"))), "9:00am", "7:00pm" ), array( date("d js f", strtotime("tuesday next week", strtotime("tuesday next week"))), "9:00am", "7:00pm" ), array( date("d js f", strtotime("wednesday next week", strtotime("wednesday next week"))), "9:00am", "7:00pm" ), array( date("d js f", strtotime("thursday next week", strtotime("thursday next week"))), "9:00am", "7:00pm" ), array( date("d js f", strtotime("friday next week", strtotime("friday next week"))), "9:00am", "7:00pm" ), array( date("d js f", strtotime("saturday next week", strtotime("saturday next week"))), "9:00am", "7:00pm" ), array( date("d js f", strtotime("sunday week", strtotime("sunday next week"))), "9:00am", "7:00pm" ) ), ); $sql[0] = "select * `tbl_opening_exceptions` `exception_date` >= '".date("y-m-d", strtotime("monday week"))."' , `exception_date` <= '".date("y-m-d", strtotime("sunday week"))."'"; $sql[1] = "select * `tbl_opening_exceptions` `exception_date` >= '".date("y-m-d", strtotime("monday next week"))."' , `exception_date` <= '".date("y-m-d", strtotime("sunday next week"))."'"; $sql[2] = "select * `tbl_opening_exceptions` `exception_date` >= '".date("y-m-d", strtotime("monday next week", strtotime("monday next week")))."' , `exception_date` <= '".date("y-m-d", strtotime("sunday next week", strtotime("sunday next week")))."'"; $i=0; foreach($sql $string) { $result = mysql_query($string) or die(mysql_error()); $r = mysql_fetch_array($result); foreach($dates[$i] &$week) { if($week[0] == date("d js f", strtotime($r["exception_date"]))) { $week[1] = date("g:ia", strtotime($r["exception_opening"])); $week[2] = date("g:ia", strtotime($r["exception_closing"])); } } ++$i; } $html = ""; $i = 0; //print_r($dates); foreach($dates $week) { print_r($week); if($i == 2) { $html .= '<table cellpadding="5" cellspacing="2" border="0" class="kc_ot_openingtable last">'; } else { $html .= '<table cellpadding="5" cellspacing="2" border="0" class="kc_ot_openingtable">'; } $html.= '<tr class="kc_ot_weekcommence"> <td colspan="3">week commencing '.$week[0][0].'</td> </tr> <tr class="kc_ot_openingtabletitle"> <td class="day">day</td> <td class="open">open</td> <td class="closed">closed</td> </tr>'; foreach($week $day) { $html .= '<tr> <td>'.$day[0].'</td> <td class="open">'.$day[1].'</td> <td class="closed">'.$day[2].'</td> </tr>'; } $html .= '</table>'; ++$i; } return $html; }
tl;dr - references evil!
the problem lies here:
foreach ($dates[$i] &$week) { // updates based on database values } foreach ($dates $week) { // generate html week data } after first loop finishes, last week still reference , $dates[count($dates) - 1]. inside second loop, $week gets assigned each element of $dates in turn.
when comes last element, $week gets assigned , recursive structure created.
the fix simple:
foreach ($dates[$i] &$week) { // updates based on database values } unset($week); // remove reference alternatively:
foreach ($dates[$i] $week) { // updates based on database values if (<some condition>) { $dates[$i][1] = 'foo'; $dates[$i][2] = 'bar'; } }
Comments
Post a Comment