如何将数组值(例如i ++中的值)与数组中的键进行匹配

时间:2016-08-12 04:50:57

标签: php multidimensional-array

`我正在尝试获得此输出。我有一个数据库表,我已经保存了商店营业时间和从星期一到星期日的营业时间。我能够在浏览器上获得记录,但我正在尝试对那天的日期进行排序。如果今天是星期五,那么星期五应该首先显示,然后在一周的其余时间显示。代码是:

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

2 个答案:

答案 0 :(得分:0)

我不认为你想在$ combine上使用foreach, 你只想要一个这样的循环:(我认为这样可行):

    for ($i=0;$i<7;$i++)
        $html .= "<tr>";
        $html .= "<td>". $datetime->format('D')."</td>";
        $html .= "<td>".$i."</td>";
        $html .= "<td>".$combine[$i]."</td>";
        $html .= "</tr>";

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

答案 1 :(得分:0)

我想我已经破解了它:)

所以这里: -

String ymbermoot2 = ""
        + Integer.parseInt(Pikkus.getText().toString())
        + Integer.parseInt(Laius.getText().toString());

ymbermoot1.setText(ymbermoot2);