Trouble with checked() for array of multiple checkboxes

Question

I can’t get the checked() to return what I expect.

Data:

Options:  = array (
'baseball' => 'Baseball',
'golf' => 'Golf',
'hockey' => 'Hockey',
)

Values:  = array (
'baseball' => 'Baseball',
'golf' => '',
'hockey' => '',
)

I’m using this code to try to return ‘checked=”checked”‘

foreach ( $options as $key => $title ) {
    rpq_plugin_debug_var( $key, 'Key: ' );
    rpq_plugin_debug_var( $title, 'Title: ' );
    $checked = checked((in_array($key, $values)), true, false);

I’d expect the baseball option to return ‘checked=”checked”‘ but it returns ”.

I’ve also tried this, which actually makes more sense to me, with no success, either.

$checked = checked((in_array($title, $values)), true, false);

Where is my error?

Many thanks.

EDIT: See comments below this post for answer.

  1. Make $values array not associative.

       Values:  = array (
    'baseball',
    'golf'
    
    )
    
  2. Use this line to identify checkmarks

    $checked = checked((in_array($key, $values)), true, false);
    
0
, , user41091 4 years 2017-07-05T15:00:32-05:00 0 Answers 80 views 0

Leave an answer

Browse
Browse