[英]PHP array of arrays. Checkboxes to be ticked depending on inner array values
I have arrays of Strings(that are each a combination of numbers "1" to "20") inside another array in PHP
. 我在PHP
另一个数组中有Strings数组(每个数组都是数字“ 1”至“ 20”的组合)。 I want to pre-tick the corresponding checkboxes (1 - 20) for each of my pages (these start at 0 and go on for potentially very many).This way the user can go to a page and see which boxes have been selected before. 我想为我的每个页面预选相应的复选框(1-20)(这些复选框从0开始并可能持续很多),这样用户可以转到页面并查看之前已选择了哪些复选框。
For example: 例如:
Array [0]
is {1, 2, 4}
so these checkboxes should be checked when the user arrives at page 0. Array [0]
为{1, 2, 4}
因此当用户到达页面0时应选中这些复选框。
This is what I have so far to try and get the values of the inner array: 到目前为止,这是我尝试获取内部数组的值的方法:
foreach ($categoriesArr as $val) {
if (is_array($val)) {
foreach ($val as $innerVal) {
// See which checkboxes are checked.
if ($innerVal === "1") {
$cb1 = true;
} else {
$cb1 = false;
}
}
}
}
I know I can use <?php if ($cb1) echo "checked" ?>
in the checkbox HTML
to show it as ticked. 我知道我可以在复选框HTML
使用<?php if ($cb1) echo "checked" ?>
将其显示为已打勾。
This is ok but it obviously loops through all the arrays and $cb1
will eventually end up as whatever the last arrays value is. 没关系,但是显然循环遍历所有数组,而$cb1
最终将以最后一个数组值结束。
Should I introduce yet another array to store each individual pages checkbox values in? 我是否应该引入另一个数组来存储每个页面的复选框值? I am potentially dealing with thousands of entries so would like to keep any extra assignments to a minimum. 我可能要处理成千上万的条目,因此希望将任何额外的任务保持在最低限度。
我建议使用保存页面索引的变量,然后将其包含在每个复选框中: <?php if(is_array($categoriesArr[$pageIndexVariable]) && $categoriesArr[$pageIndexVariable][$checkboxNameOrIdOrwhatever])
echo“ checked “?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.