如何简化此代码?

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.

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

Try

$arr = array('remote_id','remote_name','remote_object_id','remote_object_name');

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') ){
  foreach($arr as $ar){
    $record_to_include[$ar] = (isset($ind_port_record[$ar]) && isset($record_to_include['remote_id']))?$ind_port_record[$ar]:NULL;
  }
}

First off, simplify the if()

You currently have a lot of conditions

(strtoupper($ind_port_record['port_name']) == 'GI/2' || strtoupper($ind_port_record['port_name']) == 'G2' || strtoupper($ind_port_record['port_name']) == 'GI2')

Let's make that check an array

in_array( strtoupper($ind_port_record['port_name'], array('GI/2','G2','GI2')) )

Now to check if $record_to_include are not uninitialized or NULL

Let's loop through the array and do a simple check.

foreach($record_to_include as $record => $value) {
    $record_to_include[$record] = is_null($value) OR !isset($record_to_include[$record]) ? '' : $value;
}
$gi_constraint = array('GI/2', 'G2', 'GI2'); // you are checking one and the same variable for different values, so you can use in_array here
 foreach ($parent_data as $ind_port_record) {
     if (isset($ind_port_record['port_name'])  && in_array(strtoupper($ind_port_record['port_name']), $gi_constraint)){
         foreach ($ind_port_record as $k=>$v) {
            $record_to_include[$k] = $v; // as they have the same keys, you can specify the key and assign to the value of $in_port_record
         }
         break;
    }
}

//make sure you have something in remote object details
if (!isset($record_to_include['remote_id']) ){
    foreach ($record_to_include as $k => &$v) {
        $v = ''; // when using reference, it will change the original array
    }                         
}

Explanations in the code