我正在编写一个PHP库,我想在其中创建一个在IF
语句中执行的动态字符串。以下是情景。
控制器代码
$config = [
'option' => [
'exclude' => [
'system_account' => 1,
'postable_account' => 0
]
]
];
$drop_down_generator = new DropDownOptionsGenerator($config);
$drop_down_generator->generateMarkup($result);
图书馆代码
$exclude = $this->config['option']['exclude'];
$exclude_condition = '';
if (!empty($exclude)) {
//Generating IF Statemnet Condition String. Multiple Condtions can be sent from the Controller
foreach ($exclude as $key => $condition_value) {
$exclude_condition .= (empty($exclude_condition)) ? '$row->' . $key . ' == "' . $condition_value . '"' : ' && $row->' . $key . ' == "' . $condition_value . '"';
}
}
foreach ($data as $key => $row) {
// Here is the Problem, As you can see below the output of the $exclude_condition variable containing $row variable
// which is not executing in the "IF" statement
// Output of $exclude_statement => $row->system_account == "1" && $row->postable_account == "0"
if (! $exclude_condition) {
$output .= sprintf($this->option_format, $row->$value, '', $row->$label);
}
}
简而言之:如何在IF
条件中执行包含多个变量的字符串?
OR
建议我解决问题的另一个最佳解决方案。