Support

Account

Home Forums General Issues [Checkbox] How to display all item as check uncheck Reply To: [Checkbox] How to display all item as check uncheck

  • So the original answer was written before this was an option, in fact, the documentation here for the checkbox does not give what the array looks like and I’ve never used this option. The returned array looks something like this

    
    Array
    (
        [0] => Array
            (
                [value] => 1
                [label] => One
            )
    
        [1] => Array
            (
                [value] => 2
                [label] => Two
            )
    
        [2] => Array
            (
                [value] => 3
                [label] => Three
            )
    
    )
    

    Given that you need to loop through both the choices and the values

    
    
    $values = get_field('checkbox_field_name');
    $field = get_field_object('checkbox_field_name');
    $choices = $field['choices'];
       foreach ($choices as $choice_value => $choice_label) {
          echo $choice_label,': ';
          foreach ($values as $value) {
            if ($value['value'] == $choice_value) {
              echo 'checked';
            }
          }// end foreach $values
       } // end foreach $choices