首页 > 解决方案 > 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;
}

标签: phparrays

解决方案


这是工作代码。玩得开心....我肯定会的:)

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;
    
}

推荐阅读