user2496754 user2496754 - 3 months ago 9
PHP Question

How can I match the array values (e.g values from i++) to the keys in arrays

`I am trying to get this output. I have a db table where i have saved store opening hours and closing hours from monday to sunday. I am able to get the record on the browser but i am trying to sort that dates against the day. If today is friday, then friday should be shown first and then following rest of the week. The code is:

<?php

$wdays = array( 0 => "Monday" , 1 => "Tuesday", 2 => "Wednesday", 3 => "Thursday", 4 => "Friday", 5 => "Saturday", 6 => "Sunday");

$times = array(
0 => date( $row_DetailRS1['monO'] ). " ". date( $row_DetailRS1['monC'] ),
1 => date( $row_DetailRS1['tueO'] ) . " ". date( $row_DetailRS1['tueC'] ),
2 => date( $row_DetailRS1['wedO'] ) . " ". date( $row_DetailRS1['wedC'] ),
3 => date( $row_DetailRS1['thurO'] ) . " ". date( $row_DetailRS1['thurC'] ),
4 => date( $row_DetailRS1['friO'] ) . " ". date( $row_DetailRS1['friC'] ),
5 => date( $row_DetailRS1['satO'] ) . " ". date( $row_DetailRS1['satC'] ),
6 => date( $row_DetailRS1['sunO'] ) . " ". date( $row_DetailRS1['sunC'] ) );

$combine = array_combine($wdays, $times);

$html = "<table>";
$html .= "<tr><td>Sl.No</td><td>Days</td><td>Business Hours</td></tr>";
$i = 0;

while (true) {
if ($i === 7) break;

if ($datetime->format('N') === '7' && $i === 0) {
$datetime->add(new \DateInterval('P1D'));
continue;
}

echo $datetime->format('D') . $listItem[1] . "<br/>";
$listItem = array('<li>', '</li>');

$datetime->add(new \DateInterval('P1D'));
$i++;

echo "Value for i: ".$i. "and the day is: ". $datetime->format('D') . "<br/><br/><br/><br/> " ;

}

foreach ($combine as $wdays => $times):

$html .= "<tr>";
$html .= "<td>". $datetime->format('D')."</td>";
$html .= "<td>".$i."</td>";
$html .= "<td>".$times."</td>";
$html .= "</tr>";

$datetime->add(new \DateInterval('P1D'));
$i++;

endforeach;
$html .= "</table>";
echo $html;



Answer

I think I have cracked it:)

so here it goes:-

 <?php
  $datetime = new \DateTime();

  $listItem = array('<li class="active">', '</li>');



  $times = array(
    1 => date( $row_DetailRS1['monO'] ). " ". date( $row_DetailRS1['monC'] ),
    2 =>  date( $row_DetailRS1['tueO'] ) . " ". date( $row_DetailRS1['tueC'] ),
    3 =>  date( $row_DetailRS1['wedO'] ) . " ". date( $row_DetailRS1['wedC'] ),
    4 =>  date( $row_DetailRS1['thurO'] ) . " ". date( $row_DetailRS1['thurC'] ),
    5 =>  date( $row_DetailRS1['friO'] ) . " ". date( $row_DetailRS1['friC'] ),
    6 =>  date( $row_DetailRS1['satO'] ) . " ". date( $row_DetailRS1['satC'] ),
    7 =>  date( $row_DetailRS1['sunO'] ) . " ". date( $row_DetailRS1['sunC'] ) );





  $html = "<table>";
  $html .= "<tr> <td>Days</td> <td> </td> <td></td>   <td>Business Hours</td></tr>";
  $i = 1;





  foreach ($times as $key => $value):



    $m = $datetime->format('N');


      $html .= "<tr>";
      $html .= "<td>". $datetime->format('D')."</td>";
      $html .= "<td>". " "."</td>";
      $html .= "<td>". " "."</td>";
      $html .= "<td>".$times[$m]."</td>";
      $html .= "</tr>";

      $datetime->add(new \DateInterval('P1D'));



      $i++;




  endforeach;
  $html .= "</table>";
  echo $html;



 ?>
Comments