您好如何在数组中执行此操作。我希望REMARKS显示单词" Stopped"当速度低于10Kph
时这是我的代码
array(
'account' => $rowmaxtec['ACCOUNT'],
'trxdate' => $rowmaxtec['TRXDATE'],
//'trxtime' => date('H:i:s',strtotime("+8 Hours",strtotime($rowmaxtec['gpsdate'] . ' ' . $rowmaxtec['gpstime']))),
// 'trxtime' => $gpsdatetimeg,
'trxtime' => $philtime,
'long' => $rowmaxtec['LONG'],
'lat' => $rowmaxtec['LAT'],
'location' => callback($rowmaxtec['LOCATION']),
'direction' => $rowmaxtec['DIRECTION'],
'compass' => $rowmaxtec['COMPASS'],
'id' => $rowmaxtec['ID'],
'events' => $rowmaxtec['Events'],
'remarks' => $rowmaxtec['REMARKS'], -----------------------REMARKS
'status' => $rowmaxtec['devicestatus'],
'kmrun' => $kmrun,
'speed' => $rowmaxtec['speed'], <------if this is less than 10kph then REMARKS must be masked/overwritten by the word Stopped
'totalkm' => $total,
'engine' => $rowmaxtec['ENGINE']
);
很抱歉,因为这是我第一次在数组中执行if else语句。
答案 0 :(得分:10)
使用三元运算符
'remarks' => ($rowmaxtec['speed'] < 10) ? 'Stopped' : $rowmaxtec['REMARKS'],
如果你要处理$rowmaxtec['speed'] < 10
的格式(即如果它不是一个简单的整数), $rowmaxtec['speed']
可以用函数替换
快速举例,如果你考虑10Kph&#39; :
function speed_check ($speed, $check_value = 10) {
return substr($speed, 0, -3) < $check_value;
}
'remarks' => (speed_check($rowmaxtec['speed'])) ? 'Stopped' : $rowmaxtec['REMARKS'],
答案 1 :(得分:0)
您可以尝试使用if shorthands将其全部内联写入:
'remarks' => (($rowmaxtec['speed'] < 10)?"STOP":$rowmaxtec['remarks'])
答案 2 :(得分:0)
如果符合以下条件,您可以做简写:
$rowmaxtec['speed'] <= 10 ? "STOPPED" : $rowmaxtec['REMARKS'];
此处提供更多信息:
http://davidwalsh.name/php-ternary-examples
所以:
array(
'account' => $rowmaxtec['ACCOUNT'],
'trxdate' => $rowmaxtec['TRXDATE'],
//'trxtime' => date('H:i:s',strtotime("+8 Hours",strtotime($rowmaxtec['gpsdate'] . ' ' . $rowmaxtec['gpstime']))),
// 'trxtime' => $gpsdatetimeg,
'trxtime' => $philtime,
'long' => $rowmaxtec['LONG'],
'lat' => $rowmaxtec['LAT'],
'location' => callback($rowmaxtec['LOCATION']),
'direction' => $rowmaxtec['DIRECTION'],
'compass' => $rowmaxtec['COMPASS'],
'id' => $rowmaxtec['ID'],
'events' => $rowmaxtec['Events'],
'remarks' => $rowmaxtec['speed'] <= 10 ? "STOPPED" : $rowmaxtec['REMARKS'], -----------------------REMARKS
'status' => $rowmaxtec['devicestatus'],
'kmrun' => $kmrun,
'speed' => $rowmaxtec['speed'],
'totalkm' => $total,
'engine' => $rowmaxtec['ENGINE']
);