Skip Navigation

[Resolved] Turn Checkboxes Object into Array

This support ticket is created 6 years, 5 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 4 replies, has 2 voices.

Last updated by aaronM-9 6 years, 5 months ago.

Assisted by: Nigel.

Author
Posts
#919633

Can you tell me how I can programmatically take a checkboxes value and turn it into an array of checked items? Searching around, I can see this is probably done by types_render_field but I'm not sure exactly how to use it. For example...

$be_occurrence_weekdays = get_post_meta($post_id, 'wpcf-event-occurrence-weekday', false);
$be_occurrence_weekdays = array(types_render_field( ???, array( "separator" => ", " ) ));

Not sure if I'm on the right track. Thanks.

- Aaron

#919789

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Aaron

The implementation of checkboxes fields is... not so nice.

You can get the list of values of checked items as a comma-separated list (using types_render_field with only the field slug and without additional arguments), and then use PHP explode to convert that list into an array (hidden link).

That works well if you have stored meaningful values for the checkboxes, e.g. Monday -> monday, Tuesday -> tuesday.

If you just save 1 to denote checked for each option then you are just going to get back a comma-separated list of 1s, which isn't very helpful.

#919884

Hi Nigel,

Thanks. Sounds like it should work out. The value of the checkboxes is the integer PHP assigns to that day of the week, so the array should be meaningful.

Regarding the code, can you comment more specifically on how I need to use the types_render_field (line 2 in the code I pasted)? I wasn't sure from the documentation how exactly I should use the function.

- Aaron

#920203

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Aaron

The checkboxes field is documented here: https://toolset.com/documentation/customizing-sites-using-php/functions/#checkboxes

It says "With no attributes set, checkboxes will return a comma separated list of values stored for each checked checkbox".

So if you have a checkboxes field with a slug of my-checkboxes then you can assign the comma-separated list of checked items to the variable $list like so:

$list = types_render_field( 'my-checkboxes' );

You can then use explode to convert that string to an array.

#920263

Hi Nigel,

Thanks for the help. I was able to get this working, although I had to add some additional information to the code you provided in order to make it work. Here is what I ended up with:

		  $be_occurrence_weekday_list = types_render_field('event-occurrence-weekday', array("separator" => ", ", "id" => $post_id));
		  $be_occurrence_weekdays = explode(", ", $be_occurrence_weekday_list);
		  var_dump($be_occurrence_weekdays);
		  exit();

Cheers.

- Aaron