PHP多条件问题
我试图根据以下条件显示引导警报框PHP多条件问题,php,arrays,if-statement,Php,Arrays,If Statement,我试图根据以下条件显示引导警报框 // These arrays can be empty if not set $operations = [] // array of operations $positions = [] // array of posisionts $employees = [] // array of employess 现在,如果这些数组中有任何一个是空的,或者当前员工在各自的数组中,我想显示警报框。我一直在寻找一种强有力的方法。我相信简单的if(){}else{}
// These arrays can be empty if not set
$operations = [] // array of operations
$positions = [] // array of posisionts
$employees = [] // array of employess
现在,如果这些数组中有任何一个是空的,或者当前员工
在各自的数组中,我想显示警报框。我一直在寻找一种强有力的方法。我相信简单的if(){}else{}
条件可能不起作用。就像我试过的那样
if (
( empty( $operations ) || in_array( $this->cur_operation, $operations ) ) ||
( empty( $positions ) || in_array( $this->cur_position, $positions ) ) ||
( empty( $employees ) || in_array( bp_loggedin_user_id(), $employees ) )
) {
echo ac_get_alert_box( $type, $message, $dismissible, $id );
}
此情况始终显示警报
请帮我解决这个问题
谢谢
附加说明
也许我没有在上面解释清楚
我要查找的是数组是空的还是当前用户的
在这两种情况下,项都在数组中我要显示警报框
如果它是空的,则对每个人都可见,但如果它有值,则仅向具有该值的特定用户显示。您可以尝试:
if (
( empty( $operations ) || in_array( $this->cur_operation, $operations ) ) &&
( empty( $positions ) || in_array( $this->cur_position, $positions ) ) &&
( empty( $employees ) || in_array( bp_loggedin_user_id(), $employees ) )
)
好吧,你可以空着吃!其自身的
|
块中的空条件如下所示
if (
( empty($operations) || (!empty($operations) && in_array($this->cur_operation, $operations)) ) ||
( empty($positions) || (!empty($positions) && in_array($this->cur_position, $positions)) ) ||
( empty($employees) || (!empty($employees) && in_array(bp_loggedin_user_id(), $employees)) )
) {
echo ac_get_alert_box($type, $message, $dismissible, $id);
}
我希望这会有所帮助。它可能会起到以下作用:
$operations = ["foo"];
$positions = ["bar"];
$employees = ["moo"];
function shouldTrigger($array, $data = ""){
return empty($array) ? true : in_array($data, $array);
}
if(shouldTrigger($operations) || shouldTrigger($positions, "bar") || shouldTrigger($employees)){
echo("Empty array or index found");
};
您可以在
shouldTrigger()
中进一步,比如检查$data
是否为空。感谢所有为答案提供支持并尝试我帮助的人。不幸的是,没有一个解决方案对我有效,最后我自己提出了这个解决方案
如果您发现有任何可能优化代码,请提供您的输入
if ( ! is_user_logged_in() ) {
var_dump( NULL );
} else if ( empty( $operations ) && empty( $positions ) && empty( $employees ) ) {
echo ac_get_alert_box( $type, $message, $dismissible, $id );
} else if (
( in_array( $this->cur_operation, $operations ) ) &&
(
( ! in_array( $this->cur_position, $positions ) ) || ( ! in_array( bp_loggedin_user_id(), $employees ) )
)
) {
echo ac_get_alert_box( $type, $message, $dismissible, $id );
} else if (
( in_array( $this->cur_position, $positions ) ) &&
(
( ! in_array( $this->cur_operation, $operations ) ) || ( ! in_array( bp_loggedin_user_id(), $employees ) )
)
) {
echo ac_get_alert_box( $type, $message, $dismissible, $id );
} else if (
( in_array( bp_loggedin_user_id(), $employees ) ) &&
(
( ! in_array( $this->cur_operation, $operations ) ) || ( ! in_array( $this->cur_position, $positions ) )
)
) {
echo ac_get_alert_box( $type, $message, $dismissible, $id );
}
我已经尝试了
&&
,但是如果字段没有设置任何内容,则不会显示。表示如果数组为空,则表示有希望。让我试试。