Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何简化此代码?_Php - Fatal编程技术网

Php 如何简化此代码?

Php 如何简化此代码?,php,Php,我只是想知道是否有办法简化这段代码 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_

我只是想知道是否有办法简化这段代码

 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'] = '';                          
}
我只需要确保$record\u to\u include中的值不是未初始化的或空的

谢谢

试试看

$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
);
试一试

首先,简化
if()
你现在有很多条件

(strtoupper($ind_port_record['port_name']) == 'GI/2' || strtoupper($ind_port_record['port_name']) == 'G2' || strtoupper($ind_port_record['port_name']) == 'GI2')
让我们检查一个数组

in_array( strtoupper($ind_port_record['port_name'], array('GI/2','G2','GI2')) )
现在检查
$record\u to\u include
是否未初始化或
NULL
让我们在数组中循环并进行简单检查

foreach($record_to_include as $record => $value) {
    $record_to_include[$record] = is_null($value) OR !isset($record_to_include[$record]) ? '' : $value;
}
首先,简化
if()
你现在有很多条件

(strtoupper($ind_port_record['port_name']) == 'GI/2' || strtoupper($ind_port_record['port_name']) == 'G2' || strtoupper($ind_port_record['port_name']) == 'GI2')
让我们检查一个数组

in_array( strtoupper($ind_port_record['port_name'], array('GI/2','G2','GI2')) )
现在检查
$record\u to\u include
是否未初始化或
NULL
让我们在数组中循环并进行简单检查

foreach($record_to_include as $record => $value) {
    $record_to_include[$record] = is_null($value) OR !isset($record_to_include[$record]) ? '' : $value;
}
守则内的解释


代码中的解释

仅此代码
-您在哪里检查值是否为
NULL
?我不知道如何使用$record\u将其包含在整个代码中,但最好将其封装在类中。看起来你处理它更像一个对象而不是数组。你能不能改变顺序,把第二个块放在上面(如果之前没有初始化,可能没有
if
条件)?这四个(
remote.*
)是否是
$record\u to\u include
的唯一数组键?
仅此代码
-您在哪里检查值是否为
NULL
?我不知道如何使用$record\u将其包含在整个代码中,但可能最好将其封装在一个类中。看起来你处理它更像一个对象而不是数组。你能不能改变顺序,把第二个块放在上面(如果之前没有初始化,可能没有
if
条件)?这四个(
remote.*
)是
$record\u to\u include
的唯一数组键吗?+1用于初始化record\u to\u include。我早该想到的+1用于将记录\u初始化为\u include。我早该想到的!