我想从一个复选框中获取一个数组,每个记录有一个 [id] 和一个 [date]。
这是我的实际 HTML / PHP :
<input type="checkbox" name="collection[][id]" value="<?php echo $row['id']; ?>" />
<input type="hidden" name="collection[][date]" value="<?php echo date(Ymd); ?>" />
我明白了:
Array
(
[0] => Array
(
[id] => 544826
)
[1] => Array
(
[date] => 20170426
)
[2] => Array
(
[id] => 608555
)
[3] => Array
(
[date] => 20170426
)
)
我想要这个:
Array
(
[0] => Array
(
[id] => 544826
[date] => 20170426
)
[1] => Array
(
[id] => 608555
[date] => 20170426
)
)
请问,我该怎么办?
为什么不使用 ID 作为复选框的索引?这样你只需要遍历列表。IE:
<input type="checkbox" name="collection[<?php echo $row['id']; ?>]" value="<?php echo date(Ymd); ?>" />
这样你就可以收集以下数组(对于那些打勾的):
$collection = [
'id1' => 'date1',
'id2' => 'date2',
...
]
更简单,更优雅。希望这可以帮助
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句