php - PHP数组中的数据条件AND而不是OR
问题描述
下面的代码输出如下日期条件:
data-conditions="[[{"field":"field_5ed4181bd63dc","operator":"==","value":"1"},{"field":"field_5ed4178dd63d7","operator":"!=","value":"20200701"}],[{"field":"field_5ed4181bd63dc","operator":"==","value":"1"},{"field":"field_5ed4178dd63d7","operator":"!=","value":"20200702"}]]"
简单来说,它输出如下数据条件:
if field_5ed4181bd63dc == 1 AND DATE != 20200701 OR if field_5ed4181bd63dc == 1 AND DATE != 20200701 OR ...
等等。
所以它应该做的是在 field_5ed4181bd63dc == 1 时检查 BLACKOUT 日期
但在这种情况下,无论您选择 20200701 还是 20200702,BLACKOUT 日期都不会产生任何影响,因为由于 OR 运算符,它始终满足其中一个条件。
我想代码需要稍微改写一下,所以它会输出这样的数据条件:
if field_5ed4181bd63dc == 1 AND DATE != 20200701 AND DATE != 20200702 AND DATE != ...
等等。
有人可以帮我弄这个吗?
// Apply conditions to fields
add_filter('acf/prepare_field/name=booking_time_session_1', 'yl_check_booking_setting_exceptions_session_1');
function yl_check_booking_setting_exceptions_session_1($field){
$conditions = array();
if (have_rows('booking_setting_exceptions', 'booking_settings')) {
while (have_rows('booking_setting_exceptions', 'booking_settings')) {
the_row();
if (get_sub_field('booking_setting_exceptions_session') == '1') {
$date = date_i18n('Ymd', strtotime(get_sub_field('booking_setting_exceptions_date', 'booking_settings')));
if (empty($date)) {
// no date, skip this row
continue;
}
// Add the condition to the field
$conditions[] =
array(
array(
'field' => 'field_5ed4181bd63dc', // Time field session 1 in the form
'operator' => '==', // If Value is same, then show the field
'value' => '1', // Compare against session option page value
),
array(
'field' => 'field_5ed4178dd63d7', // Date field in the form
'operator' => '!=', // If Value is different, then show the field
'value' => $date, // Compare against date option page value
),
);
}
} // end while have_rows
} // end if have_rows
$field['conditional_logic'] = $conditions;
// Return
return $field;
}
解决方案
这是工作代码。玩得开心....我肯定会的:)
add_filter('acf/prepare_field/name=booking_time_session_1', 'yl_check_booking_setting_exceptions_session_1');
function yl_check_booking_setting_exceptions_session_1($field){
// Reset conditions
// Warning: Conditions set in the UI won't be compatible!
$conditions = array();
// Creating Base Condition
$conditions[] = array(
array(
'field' => 'field_5ed4181bd63dc',
'operator' => '==',
'value' => '1',
)
);
if(have_rows('booking_setting_exceptions', 'booking_settings')):
while(have_rows('booking_setting_exceptions', 'booking_settings')): the_row();
// Check: Booking Settings Exceptions Session
if(get_sub_field('booking_setting_exceptions_session') != '1')
continue;
$date = date_i18n('Ymd', strtotime(get_sub_field('booking_setting_exceptions_date', 'booking_settings')));
// Check: Booking Settings Exceptions Date
if(empty($date))
continue;
// Adding a new AND condition inside Base condition, at the index = 0
$conditions[0][] = array(
'field' => 'field_5ed4178dd63d7',
'operator' => '!=',
'value' => $date,
);
endwhile;
endif;
// Setting the full condition
$field['conditional_logic'] = $conditions;
return $field;
}
推荐阅读
- android - 基于字符串数组项,将相同的项显示到下一个片段
- angular - 如何将 routerLink 与 innerHTML 一起使用
- c - 在这个函数中正确的指针没有返回到调用函数
- mysql - 在我的情况下,如何在没有 GROUP BY 的情况下使用 GROUP_CONCAT?
- tsql - t-sql 函数,如 sum(x) 过滤器(条件)的“过滤器”(按
- reactjs - 在redux中我的动作被调用但不是reducer,我该如何解决它?
- sql - 无法使用自动生成的 ID 保存模型记录
- node.js - 如何为访问 aws 资源的函数编写单元测试?
- perl - 在 IPC::ShareLite 对象中存储多个项目
- javascript - 如何计算 JavaScript 中两个字符串中的常见字符?