我有一大堆复选框输入,它们都具有相同的名称属性。我想保存它们的值(如果选中则为1,如果不是则为0,因为顺序很重要,我需要以正确的顺序输出它们)作为一个不按预期方式运行的数组。
<input type="checkbox" name="feature[]" id="feature-a"/>
<input type="checkbox" name="feature[]" id="feature-b"/>
<input type="checkbox" name="feature[]" id="feature-c"/>
<input type="checkbox" name="feature[]" id="feature-d"/>
等
foreach( $_POST['feature'] as $feature ) {
if( isset( $feature ) && '' !== $feature ) {
$feature = 1;
}
else {
$feature = '';
}
$features = array();
array_push($features, $feature);
}
update_term_meta( $term_id, 'features', $features );
//This always outputs a:1 {i:0;i:1;} for some reason! No matter what I check
我缺少什么?
feature[]
作为名称属性$feature
什么都没有用。我的循环中是否有错误我无法看到?
答案 0 :(得分:1)
foreach($ _POST [&#39; feature&#39;] as $ feature){
if( isset( $feature ) && $feature !=='' ) {
$feature = 1;
}
else {
$feature = 0;
}
$features = array();
array_push($features, $feature);
} update_term_meta($ term_id,&#39; features&#39;,$ features);