开发者

Converting associative POST data input into a serialized array

I'm开发者_运维问答 using a multidimensional array in a form and I want to parse these values into a serialized array to be stored in my database. Here's the relevant section of my HTML form:

<select name="week_days[monday]"><option value=""></option><option value="preferred">Preferred</option><option value="restricted">Restricted</option></select>
<select name="week_days[tuesday]"><option value=""></option><option value="preferred">Preferred</option><option value="restricted">Restricted</option></select>
<select name="week_days[wednesday]"><option value=""></option><option value="preferred">Preferred</option><option value="restricted">Restricted</option></select>
<select name="week_days[thursday]"><option value=""></option><option value="preferred">Preferred</option><option value="restricted">Restricted</option></select>

I want to then create an array in my model called week_days. An example of what I want the array to look like is this:

array('monday'=>'preferred','tuesday'=>'','wednesday'=>'restricted','thursday'=>'');

I will then serialize() this array for storage in my database. How do I go about converting the multidimensional array input into an array for serialization?


When you submit this in a form, you will get a $_POST['week_days'] which will be equal to your array in your question


Here is some example code to demonstrate:

<pre><?php

if ($_POST['week_days']) {
    print_r($_POST);
    print_r(serialize($_POST['week_days']));
}

?></pre>
<form method="post">
<select name="week_days[monday]"><option value=""></option><option value="preferred">Preferred</option><option value="restricted">Restricted</option></select>
<select name="week_days[tuesday]"><option value=""></option><option value="preferred">Preferred</option><option value="restricted">Restricted</option></select>
<select name="week_days[wednesday]"><option value=""></option><option value="preferred">Preferred</option><option value="restricted">Restricted</option></select>
<select name="week_days[thursday]"><option value=""></option><option value="preferred">Preferred</option><option value="restricted">Restricted</option></select>
<input type="submit"/>
</form>

Which outputs (based on what's selected):

Array
(
    [week_days] => Array
        (
            [monday] => preferred
            [tuesday] => restricted
            [wednesday] => restricted
            [thursday] => preferred
        )

)
a:4:{s:6:"monday";s:9:"preferred";s:7:"tuesday";s:10:"restricted";s:9:"wednesday";s:10:"restricted";s:8:"thursday";s:9:"preferred";}


Well... Quite straightforward really if I've understood you correctly:

$serialized = serialize ( $_POST['week_days'] );

will do what you want...

That said, a couple of notes:

  • You might want to make a new table where you store the days and have a boolean column for preferred or not
  • This is not a multi-dimensional array, but an associative array =)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜