1

I have a checkbox field for dining cuisine types and I see that the checked options are stored as a JSON-encoded array in the content table. If I have an element that uses this field, is there a way I can just get that JSON-encoded value as an array in PHP? Any time I try to get the value of the field, I'm getting the entire MultiOptionsFieldData object which isn't desired in this case. I just want that array of checked values that's stored in the corresponding content table column.

enter image description here

I don't want to write any loops. I just want access to that actual column value from my element in PHP.

I don't want to write any additional queries to get that column value either. Surely, there must be a way to get the actual field value for checkbox fields. Right?

1

MultiOptionsFieldData extends PHP's ArrayObject class.

You could get the values from it by casting it to an array like so:

$results = (array)$mofd;

That would give you an array of OptionData classes. If you really didn't want to loop through them, you could do something like:

$func = static function($ofd) {
    return $ofd->value;
};

$results = array_map($func, (array)$mofd);
  • Yeah, getting the array of OptionData instances was as close as I got but I was hoping for just the array of string values. Thanks, Brad. I think a method to obtain the raw value on the MultiOptionsFieldData class might be helpful in situations like this. Maybe something like: $entry->getFieldValue('diningCuisine')->rawValue() – Benjamin Kohl Jul 29 at 20:15
1

Another option for getting the string values as an array after typecasting the MultiOptionsFieldData object as an array:

$values = ArrayHelper::getColumn(ArrayHelper::toArray( (array) $entry->getFieldValue('diningCuisine') ), 'value');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.