Support

Account

Home Forums General Issues ACF Checkbox Field in array()

Helping

ACF Checkbox Field in array()

  • Hi ACF Community,

    I have a small problem. I have a checkbox field with the numbers from 1 – 60. You can select 60 fields.

    Usually you output the fields in a foreach loop, unfortunately I need the selected fields in an array ().

    In the end it should look like this:
    $seconds = array ("1" => 0, "2" => 0, "3" => 0, "4" => 0, "5" => 0, "20" => 0, "21" => 0, "22" => 0, "23" => 0, "24" => 0, "25" => 0, "26" => 0, "27" => 0, "28" = > 0, "29" => 0, "30" => 0, "60" => 0,);

    That does not work:
    $seconds = array (get_field_objects('second-field'));

    That doesn’t work either

    $values = array();
    $time = get_field('second-field');
    foreach ($time as $second) {
        $values[] = '"'.$second.'" => 0, '; 
    }
    $totalvalue = implode("\n", $values);
    $seconds = array ($totalvalue)

    Does anyone know how I can get the checkbox fields into the array?

    Many thanks and best regards Christoph

  • Hi @unbekannter

    The below will output the selected checkboxes into an array:

    
    $values = array();
    $time = get_field('second-field');
    foreach ($time as $second) {
        $values[] = '"'.$second.'" => 0'; 
    }
    
    echo implode(',', $values);
    

    Do you need it as an associative array? I’m trying to work out it out.
    You asked for checkboxes to be an array but the example you give of how it needs to be is more like an associative array (I believe but could be wrong!).

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.