简体   繁体   中英

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 . 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.

For example:
Array [0] is {1, 2, 4} so these checkboxes should be checked when the user arrives at page 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.
This is ok but it obviously loops through all the arrays and $cb1 will eventually end up as whatever the last arrays value is.
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 “?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM