我在foreach循环中有以下条件语句。我希望检查所有条件语句。我希望通过使用switch case语句可以完成它。任何人都可以给我一个想法如何做到这一点?或者还有其他方法吗?有些日期有关于所有四个条件的值。他们不会检查我是否使用此代码。由于我是新手,我很难弄清楚如何做到这一点。如果有人能提出想法,那将是一个很大的帮助。
foreach ($query->result() as $row) {
if ($row->title == 'GK' && $row->type == 'AM') {
$cal_data[substr($row->date_cal, 8, 2)] = '<div class="gk_am">' . $row->title . ' ' . $row->type . '</div>';
} if ($row->title == 'GK' && $row->type == 'PM') {
$cal_data2[substr($row->date_cal, 8, 2)] = '<div class="gk_pm">' . $row->title . ' ' . $row->type . '</div>';
} if ($row->title == 'RP' && $row->type == 'AM') {
$cal_data3[substr($row->date_cal, 8, 2)] = '<div class="rp_am">' . $row->title . ' ' . $row->type . '</div>';
} if ($row->title == 'RP' && $row->type == 'PM') {
$cal_data4[substr($row->date_cal, 8, 2)] = '<div class="rp_pm">' . $row->title . ' ' . $row->type . '</div>';
}
}
答案 0 :(得分:6)
如果是条件而不是仅使用其他如果条件:
foreach ($query->result() as $row) {
if ($row->title == 'GK' && $row->type == 'AM') {
$cal_data[substr($row->date_cal, 8, 2)] = '<div class="gk_am">' . $row->title . ' ' . $row->type . '</div>';
} elseif ($row->title == 'GK' && $row->type == 'PM') {
$cal_data2[substr($row->date_cal, 8, 2)] = '<div class="gk_pm">' . $row->title . ' ' . $row->type . '</div>';
} elseif ($row->title == 'RP' && $row->type == 'AM') {
$cal_data3[substr($row->date_cal, 8, 2)] = '<div class="rp_am">' . $row->title . ' ' . $row->type . '</div>';
} elseif ($row->title == 'RP' && $row->type == 'PM') {
$cal_data4[substr($row->date_cal, 8, 2)] = '<div class="rp_pm">' . $row->title . ' ' . $row->type . '</div>';
}
}
答案 1 :(得分:1)
foreach ($query->result() as $row)
{
switch($row->title)
{
case 'GK':
switch($row->type)
{
case 'AM':
$cal_data[substr($row->date_cal, 8, 2)] = '<div class="gk_am">' . $row->title . ' ' . $row->type . '</div>';
break;
case 'PM':
$cal_data2[substr($row->date_cal, 8, 2)] = '<div class="gk_pm">' . $row->title . ' ' . $row->type . '</div>';
break;
}
break;
case 'RP':
switch($row->type)
{
case 'AM':
$cal_data3[substr($row->date_cal, 8, 2)] = '<div class="rp_am">' . $row->title . ' ' . $row->type . '</div>';
break;
case 'PM':
$cal_data4[substr($row->date_cal, 8, 2)] = '<div class="rp_pm">' . $row->title . ' ' . $row->type . '</div>';
break;
}
break;
}
}
以上代码与您给定的条件代码相同。看看吧。
建议您在switch
语句中使用conditional
语句,我们知道可能存在哪些值。
答案 2 :(得分:1)
如果你想对switch-case
做同样的事情,那么这是一种方法。
switch($row->title()){
case 'GK':
switch($row->type()){
case 'AM':
......
break;
}
break;
......
}
答案 3 :(得分:1)
foreach ($query->result() as $row) {
switch($row->title) {
case 'GK': if($row->type=='AM'){
$cal_data[substr($row->date_cal, 8, 2)] = '<div class="gk_am">' . $row->title . ' ' . $row->type . '</div>';
}else{
$cal_data2[substr($row->date_cal, 8, 2)] = '<div class="gk_pm">' . $row->title . ' ' . $row->type . '</div>';
}
break ;
case 'RP': if($row->type=='AM'){
$cal_data3[substr($row->date_cal, 8, 2)] = '<div class="rp_am">' . $row->title . ' ' . $row->type . '</div>';
}else{
$cal_data4[substr($row->date_cal, 8, 2)] = '<div class="rp_pm">' . $row->title . ' ' . $row->type . '</div>';
}
break ;
}
}
试试这个
答案 4 :(得分:1)
这看起来实际上要比它需要的复杂得多。
$all_cals = [
'GK' => [
'AM' => &$cal_data,
'PM' => &cal_data2
],
'RP' => [
'AM' => &$cal_data3,
'PM' => &cal_data4
]
];
foreach ($query->result() as $row) {
$row_cal_data = $all_cals[$row->title][$row->type];
if (isset($row_cal_data)) {
$row_cal_data[substr($row->date_cal, 8, 2)] = '<div class="'.tolower($row->title)."_".tolower($row->type).'">' . $row->title . ' ' . $row->type . '</div>';
}
}
根本没有开关声明。
答案 5 :(得分:1)
我认为接近你的情景
prepareForSegue:
答案 6 :(得分:1)
放弃整个model1_obj = xx.objects.filter(
date_generated__gte=1429142401,
date_generated__lte=1431648001,
s__icontains="28").exclude(Q(u__in=[93,95]) | Q(v__isnull=True))
等并使用foreach ($query->result() as $row) {
switch (true) {
case ($row->title == 'GK' && $row->type == 'AM'):
$cal_data[substr($row->date_cal, 8, 2)] = '<div class="gk_am">' . $row->title . ' ' . $row->type . '</div>';
break;
case ($row->title == 'GK' && $row->type == 'PM'):
$cal_data2[substr($row->date_cal, 8, 2)] = '<div class="gk_pm">' . $row->title . ' ' . $row->type . '</div>';
break;
case ($row->title == 'RP' && $row->type == 'AM'):
$cal_data3[substr($row->date_cal, 8, 2)] = '<div class="rp_am">' . $row->title . ' ' . $row->type . '</div>';
break;
case ($row->title == 'RP' && $row->type == 'PM'):
$cal_data4[substr($row->date_cal, 8, 2)] = '<div class="rp_pm">' . $row->title . ' ' . $row->type . '</div>';
break;
}
}
。通过使用switch
作为您的班级名称,您可以在一行中完成所有操作。您的变量递增(array
,strtolower()
..等)而不是数组中的新值$cal_data1
,$cal_data2
...等。它可以使您的代码更容易使用:
$cal_data['date_cal'][0]