-1

I have a serialized value in my mysql db that looks like this:

a:5:{s:4:"name";s:13:"Keith Donegan";s:3:"age";s:2:"21";s:7:"college";s:9:"Spin City";s:8:"category";s:1:"7";s:8:"checkbox";a:2:{i:0;s:1:"3";i:1;s:1:"9";}}

I can output the checkbox array fine, but I need to run it againest another set of ids using in_array but my code doesn't work, surprise! :)

echo $eirepanel_general_options['checkbox']; // used in a foreach loop

See the values 3 & 9, I need to test these.


EDIT: MAIN Code

<?php

$eirepanel_general_options_saved = $_REQUEST['eirepanel_general_options_saved'];
if(isset($eirepanel_general_options_saved))
{
    $eirepanel_general_options_name = $_REQUEST['eirepanel_general_options_name'];
    $eirepanel_general_options_age = $_REQUEST['eirepanel_general_options_age'];
    $eirepanel_general_options_college = $_REQUEST['eirepanel_general_options_college'];
    $eirepanel_general_options_category = $_REQUEST['eirepanel_general_options_category'];
    $eirepanel_general_options_checkbox = $_REQUEST['eirepanel_general_options_checkbox'];

    $eirepanel_general_options = array
    (
    'name' => $eirepanel_general_options_name,
    'age' => $eirepanel_general_options_age,
    'college' => $eirepanel_general_options_college,
    'category' => $eirepanel_general_options_category,
    'checkbox' => $eirepanel_general_options_checkbox
    );

    update_option('eirepanel_general_options', $eirepanel_general_options);
}
else
{
$eirepanel_general_options = get_option('eirepanel_general_options');
}


$categories = get_categories(); 
foreach($categories as $category)
{ 
    $eirepanel_general_options_string = $eirepanel_general_options['checkbox'];
    $eirepanel_general_options_array = explode(',', $eirepanel_general_options_string);
    echo$cat_ids = explode(',',$category->cat_ID);

    $ids = array(8, 4);

    var_dump($eirepanel_general_options_string);

    ?>


    <?php // <input name="eirepanel_general_options_checkbox[]" type="checkbox" value="<?php echo $category->cat_ID; ?>
        <?php // if($eirepanel_general_options_string == $category->cat_ID ){ echo "checked='checked'"; } <br /> ?>

        <span><?php echo $category->cat_name; ?></span>
        <input name="eirepanel_general_options_checkbox[]" value="<?php echo $category->cat_ID; ?>" type="checkbox" <?php in_array($eirepanel_general_options_string, $ids) ? 'checked="checked"' : '' ?> /> <br />

<?php }

// var_dump($eirepanel_general_options_string);


?>          
3
  • 4
    Why don't you unserialize the array first to use in_array? I don't see the problem. Commented Apr 21, 2011 at 12:15
  • Why do you declare variables only to pass them into an array? Commented Apr 21, 2011 at 12:39
  • I'm still struggling to understand what your expected outcome is, and what your actual outcome is. Which bit is broken? Commented Apr 21, 2011 at 12:40

2 Answers 2

3

unserialize is your friend here!!

run the string containing the array through that function, catch the result and work with that.

$serial = **serial array goes here**;
$array = unserialize($serial);

Then

echo $array['checkbox'];

would function as you would expect.

Use serialize to reverse the effects, if needed.

$serial = serialize($array);
8
  • Sorry, I should have mentioned, I am using Wordpress and this does this already I assume Commented Apr 21, 2011 at 12:19
  • @KeithDonegan what does the value look like if you var_dump() it right before using in_array()? That way you can be sure if Wordpress is indeed doing what you expect. Commented Apr 21, 2011 at 12:20
  • does what already? It will serialize an array when storing it in the database (I think this may be mySQL doing the leg work here, but I could be wrong). Commented Apr 21, 2011 at 12:20
  • @Treffynnon array(2) { [0]=> string(1) "7" [1]=> string(1) "6" } thanks Commented Apr 21, 2011 at 12:23
  • 1
    I think you need to put a lot more code in your question for us to really see what the issue is. Commented Apr 21, 2011 at 12:26
0

A serialised array is no longer an array but a string export of the array values. To use array functions, you need to return it to an array using unserialize(), then it is an array which you can treat like an array else, it is just a string.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.