我的mysql数据库中有一个序列化值,如下所示:
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";}}
我可以输出复选框数组,但是我需要使用in_array运行它另一组id,但我的代码不起作用,令人惊讶! :)
echo $eirepanel_general_options['checkbox']; // used in a foreach loop
参见值3& 9,我需要测试这些。
编辑:主要代码
<?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);
?>
答案 0 :(得分:3)
unserialize是你的朋友!!
通过该函数运行包含数组的字符串,捕获结果并使用它。
$serial = **serial array goes here**;
$array = unserialize($serial);
然后
echo $array['checkbox'];
会像你期望的那样发挥作用。
如果需要,请使用serialize来反转效果。
$serial = serialize($array);
答案 1 :(得分:0)
序列化数组不再是数组,而是数组值的字符串导出。 要使用数组函数,您需要使用unserialize()将其返回到数组,然后它是一个数组,您可以将其视为数组else,它只是一个字符串。