Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 告诉我那里';这是将这些复选框转换为JSON的更好方法_Php_Arrays_Json - Fatal编程技术网

Php 告诉我那里';这是将这些复选框转换为JSON的更好方法

Php 告诉我那里';这是将这些复选框转换为JSON的更好方法,php,arrays,json,Php,Arrays,Json,我正在制作一个表格,让人们选择他们喜欢的联系方式。除此之外,表单包含9个复选框(3组3个),我试图将它们作为JSON存储在数据库中 以下是表格的相关部分 <h1 style="padding-top:25px;">Communication Preferences</h1><hr /> <div class="onethird contactprefs" style="width: 28%"> <h4>Preferred Met

我正在制作一个表格,让人们选择他们喜欢的联系方式。除此之外,表单包含9个复选框(3组3个),我试图将它们作为JSON存储在数据库中

以下是表格的相关部分

<h1 style="padding-top:25px;">Communication Preferences</h1><hr />
<div class="onethird contactprefs" style="width: 28%">
    <h4>Preferred Method</h4>
    <p>From time to time, we might need to contact you regarding our service.  Which mode of contact would you prefer?</p>
    <p>
    <input type="checkbox" name="preferred[]" value="p" style="display: inline;" />Phone <!-- make these tooltips -->
    <br />
    <input type="checkbox" name="preferred[]" value="e" style="display: inline;" checked />Email
    <br />
    <input type="checkbox" name="preferred[]" value="s" style="display: inline;" />SMS
    </p>
</div>

<div class="onethird contactprefs" style="width: 28%; border-left: solid 1px #cdcdcd; border-right: solid 1px #cdcdcd; padding-left: 18px; padding-right: 15px;">
    <h4>Weather Delays</h4>
    <p>We don't mess with Mother Nature, and sometimes she forces us to cancel service.  If that happens, how should we inform you?</p>
    <p>
    <input type="checkbox" name="weather[]" value="p" style="display: inline;" />Phone
    <br />
    <input type="checkbox" name="weather[]" value="e" style="display: inline;" checked />Email
    <br />
    <input type="checkbox" name="weather[]" value="s" style="display: inline;" />SMS
    </p>
</div>

<div class="onethird contactprefs" style="width: 28%">
    <h4>Holiday Reminders</h4>
    <p>If you'd like us to send you reminders about interruptions in service due to holidays, just choose your preferred method below.</p>
    <p>
    <input type="checkbox" name="holiday[]" value="p" style="display: inline;" />Phone
    <br />
    <input type="checkbox" name="holiday[]" value="e" style="display: inline;" checked />Email
    <br />
    <input type="checkbox" name="holiday[]" value="s" style="display: inline;" />SMS
    </p>

</div>

 <!-- end contact preferences -->

提前感谢您的洞察力。

一个让它更自动化的想法(未经测试):


当然,这只适用于三个复选框组具有相同值的情况。

有一种更好的方法可以将这些复选框转换为j,因为这三个组具有相同的值。快速测试之后,第7行出现错误,因为$type未定义。我把$type改为$group,但这并没有给出我想要的东西。这是一个美妙的开始。我需要几分钟的时间来思考这个问题,但我想这会给我我想要的。谢谢当它起作用时,我会发布最终结果。@Ryan:是的,我更改了变量的名称,一定错过了那个。实际上,它应该生成与您相同的值。它非常接近,但是$preferences数组在最后一次运行foreach时被覆盖了。这是我的作品:{“首选”:{“p”:1,“e”:0,“s”:0},“天气”:{“p”:0,“e”:1,“s”:0},“假日”:{“p”:0,“e”:0,“s”:1}}}这是你的作品:{“首选”:{“p”:0,“e”:0,“s”:1},“天气”:{“p”:0,“e”:0,“s”:1},“假日”:{“p”:0,“e”:0,“s”:1},我明白了。必须是对数组的引用。我以为它管用。检查我的更新版本。我基本上移动了任务
$contact\u prefs[$group]=$preferences到底部。绝对漂亮,菲利克斯。你们让事情看起来很简单。谢谢
/* THERE MUST BE A BETTER WAY TO DO THIS */
// get the preferred data
$preferred = $this->input->post('preferred');

if(in_array('p',$preferred)){
    $pref['p'] = 1;
}else{ $pref['p'] = 0;}

if(in_array('e',$preferred)){
    $pref['e'] = 1;
}else{ $pref['e'] = 0;}

if(in_array('s',$preferred)){
    $pref['s'] = 1;
}else{ $pref['s'] = 0;}

// get the weather data
$weather = $this->input->post('weather');

if(in_array('p',$weather)){
    $wea['p'] = 1;
}else{ $wea['p'] = 0;}

if(in_array('e',$weather)){
    $wea['e'] = 1;
}else{ $wea['e'] = 0;}

if(in_array('s',$weather)){
    $wea['s'] = 1;
}else{ $wea['s'] = 0;}

// get the holiday data
$holiday = $this->input->post('holiday');

if(in_array('p',$holiday)){
    $hol['p'] = 1;
}else{ $hol['p'] = 0;}

if(in_array('e',$holiday)){
    $hol['e'] = 1;
}else{ $hol['e'] = 0;}

if(in_array('s',$holiday)){
    $hol['s'] = 1;
}else{ $hol['s'] = 0;}

$contact_prefs = array('preferred' => $pref,'weather' => $wea,'holiday' => $hol);
$contact_prefs = json_encode($contact_prefs);
$contact_prefs = array();
$groups = array('preferred', 'weather', 'holiday'); // checkbox groups

foreach($groups as $group) {
    $preferences = array('p' => 0, 'e' => 0, 's' => 0); // create default array
    $values = $this->input->post($group); // get the checked values for a group
    foreach($values as $value) {
        $preferences[$value] = 1; // if box is checked, set value to 1
    }
    $contact_prefs[$group] = $preferences;
}

$contact_prefs = json_encode($contact_prefs);