dongxin0031 2011-01-10 22:22
浏览 73
已采纳

在php中使用if语句创建默认值

I need some help with re-working the logic on this php code. What I'd like to do is build in some conditions in this foreach loop to handle empty values, null and give it some default value like 'n/a'. Its using the $class_array to match one of the cases which is read from a text file, but if I don't have either of the statuses, then make it 'n/a'.

I'm just not clear how to construct that. Here is part of the code. I'm thinking, it must be handled within this foreach condition?? Thanks.

$class_array = array(
        'Process succeeded'      => 'success',
        'Process failed'         => 'failure',
        'Review Logs for status' => 'warn',
        'Check for Errors'       => 'warn'
);

foreach ($InputFile as $line){
    preg_match_all("/([0-9])-([^=]+)=([^;]+);/", $line, $matches, PREG_SET_ORDER);
    $LineData = array();
    foreach ($matches as $information) {
        $LineData[$information[2]] = $information[3];
    }
    $timestamp = strtotime($LineData["LogDate"]." ".$LineData["StartTime"]);
    $LineData['StartTime'] = date("Y-m-d H:i:s",strtotime($LineData['StartTime']));
    $LineData['server_url'] = $server_array[$LineData['Server']];
    $LineData['status_class'] = $class_array[$LineData['Status']];

    $data[$timestamp] = $LineData;
}
  • 写回答

3条回答 默认 最新

  • duanshan3065 2011-01-10 22:40
    关注

    Do you mean that $LineData['status_class'] should be set to 'n/a' in case there is no $LineData['Status'] key in $class_array? If so, this should do:

    $status_class = 'n/a';
    if (array_key_exists($LineData['Status'], $class_array)) {
        $status_class = $class_array[$LineData['Status']];
    }
    $LineData['status_class'] = $status_class;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失