2011-04-21 85 views
-1

我在我的MySQL数据库的序列化值,看起来像这样:在序列化数组中使用in_array?

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";}} 

我可以输出的复选框阵列很好,但我需要运行它againest另一组使用in_array IDS的,但我的代码不工作,惊喜! :)

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); 


?>   
+4

为什么不首先使用in_array反序列化数组?我没有看到问题。 – 2011-04-21 12:15:34

+0

为什么你只声明变量将它们传递给数组? – 2011-04-21 12:39:16

+0

我仍然在努力理解你的预期结果是什么,以及你的实际结果是什么。哪一点坏了? – 2011-04-21 12:40:15

回答

3

unserialize这里是你的朋友!

通过该函数运行包含该数组的字符串,捕获结果并处理该结果。

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

然后

echo $array['checkbox']; 

会起到你所期望的。如果需要,可使用serialize来反转效果。

$serial = serialize($array); 
+0

对不起,我应该提到,我正在使用Wordpress,并且这已经假设我已经假设 – 2011-04-21 12:19:18

+0

@KeithDonegan如果你在使用'in_array()'之前调用了'var_dump()',那么值是什么样子?这样你就可以确定Wordpress是否确实在做你期望的。 – Treffynnon 2011-04-21 12:20:41

+0

已经做了什么?它会在将数组存储在数据库中时序列化一个数组(我认为这可能是mySQL在这里做腿部工作,但我可能是错的)。 – 2011-04-21 12:20:44

0

串行化数组不再是数组,而是数组值的字符串导出。 要使用数组函数,您需要使用unserialize()将其返回给数组,否则它就是一个可以像数组一样处理的数组,它只是一个字符串。