douzhong5902 2014-02-11 15:31
浏览 70
已采纳

如何简化此代码?

I'm just wondering if there's a way to simplify this code?

 foreach ($parent_data as $ind_port_record) {
     if (   isset($ind_port_record['port_name'])  && (strtoupper($ind_port_record['port_name']) == 'GI/2' || strtoupper($ind_port_record['port_name']) == 'G2' || strtoupper($ind_port_record['port_name']) == 'GI2') ){
            $record_to_include['remote_id'] = $ind_port_record['remote_id'];
            $record_to_include['remote_name'] = $ind_port_record['remote_name'];
            $record_to_include['remote_object_id'] = $ind_port_record['remote_object_id'];
            $record_to_include['remote_object_name'] = $ind_port_record['remote_object_name'];
            break;
        }
  }

//make sure you have something in remote object details
if ( ! isset($record_to_include['remote_id']) ){
    $record_to_include['remote_id'] = '';
    $record_to_include['remote_name'] = '';
    $record_to_include['remote_object_id'] = '';
    $record_to_include['remote_object_name'] = '';                          
}

I just need to make sure the values inside the $record _to_include are not uninitialized or NULL.

Thanks.

  • 写回答

4条回答 默认 最新

  • douniao8687 2014-02-11 15:39
    关注
    $record = array_filter($parentData, function (array $record) {
        return isset($record['port_name']) && preg_match('!^(GI/2|G2|GI2)$!i', $record['port_name']);
    });
    
    $recordToInclude = $record ? current($record) : array(
        'remote_id'          => null,
        'remote_name'        => null,
        'remote_object_id'   => null,
        'remote_object_name' => null
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM