Home › Forums › General Issues › Checkbox does not save field as array
Hi,
I’m using conditional statement for my checkbox field as per the documentation:
<?php
$llbstones = get_field( 'stonename' );
if( $llbstones && in_array('Black Onyx', $llbstones) ) {
echo do_shortcode( '[su_lightbox type="inline" src="#content-bk-onyx" class="llb-stone-popup"]Black Onyx[/su_lightbox]' );
};
if( $llbstones && in_array('Blue Topaz', $llbstones) ) {
echo do_shortcode( '[su_lightbox type="inline" src="#content-blu-topaz" class="llb-stone-popup"]Blue Topaz[/su_lightbox]' );
};
?>
It’s working in the staging site, but it’s not working when I copied everything to the live site. Here’s the warning I got:
in_array() expects parameter 2 to be array, string given
and when I enable debug, I got this notice:
Undefined index: multiple in /public_html/shop/wp-content/plugins/advanced-custom-fields/includes/fields/class-acf-field-select.php
I tried changing the code to:
<?php
if( get_field('stonename') )
{
if( in_array( 'Black Onyx', get_field('stonename') ) ) {
echo do_shortcode( '[su_lightbox type="inline" src="#content-bk-onyx" class="llb-stone-popup"]Black Onyx[/su_lightbox]' );
}
if( in_array( 'Blue Topaz', get_field('stonename') ) ) {
echo do_shortcode( '[su_lightbox type="inline" src="#content-blu-topaz" class="llb-stone-popup"]Blue Topaz[/su_lightbox]' );
}
}
?>
As it solves the issue in https://support.advancedcustomfields.com/forums/topic/conditional-statement-problem-expects-parameter/
But it’s not working for me and still shows the same error.
Running var_dump gives me this result:
string(10) "Black Onyx"
Even if I check all checkboxes, the var_dump result stays the same.
Hope someone can help me.
Thanks in advance.
I believe I see a similar issue. In my setup the checkbox is part of a repeater. The value stored in the database is clearly an array, but it is returned as string. My users can select one or more items, and I implode the returned array, to generate a set of classes.
Relevant snippet:
$categories = get_sub_field('category'); //checkbox returning value
var_dump($categories);
$category_list = $categories ? implode(' ', $categories) : '';
Stored in database: a:1:{i:0;s:10:"cat-global";}
Debug on my staging site: string(10) "cat-global"
Return output is set to ‘Value’, documentation states array is returned.
Strangly enough, also for me it seems to work on my local development environment as with debug it returns: array(1) { [0]=> string(10) "cat-global" }
Just checking if we have something in common: my prod/staging are on WPEngine.
By the way I can make my code work by switching the output to ‘Both (Array)’, and then using the first returned item. Just wondering what happened as my code has been running for a few years.
The topic ‘Checkbox does not save field as array’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.