0

I need to do an import of an array of checkboxes, but there is a value that I don't how it is determined.

The array is based on a user meta option that I created that shows a list of category checkbox options. It grabs all of the categories and creates a checkbox option that can be saved to a user's profile.

This is part of the coding I used to create the user meta option:

$args = array( 'hide_empty' => 0, 'taxonomy'=> 'category');
$categories =  get_categories($args);

if ($categories){
                        foreach ( $categories as $category ){ 
                            if(in_array($category->term_id,(array)$data)) {
                                $selected = 'checked="checked""';
                            } else {
                                $selected = '';
                            }
                            echo '<input name="user_cats[]" value="'.$category->term_id.'" '.$selected.' type="checkbox"/> '.$category->name.'<br/>';
                        }
                    }

This is what the options look like in the user profile page:

enter image description here

After doing a test save of the some of the checkbox selections, I examined the database value of the "user_cats" field, it gives me something like this:

a:2:{i:0;s:2:"20";i:1;s:3:"343";}

Now, I know what all but one of the variables stand for.

a = Total amount (quantity) of selections

i = Sequential number of the selection (*Starting with 0)

"X" = The number value within the parenthesis is the category ID

My question is, what is the s: value based on? I tried comparing to

3

1 Answer 1

2

Use the php function unserialize() on that string and you will get a proper array.

$cats = get_cats_from_database();
$cats = unserialize($cats);
        //now $cats is an array like Array(0 => 20, 1 => 343 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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