php - 在 php 中使用 in_array 时缺少一个值

标签 php html checkbox foreach nested-for-loop

我有两个数组,下面是输出。第一个数组是我的所有列表,第二个数组是我选择的用户获得的。

$specification=getgeneralSpecifications($pdo); // getting below array

Array(
    [0] => Array
        (
            [sp_id] => 1
            [specifications_name] => example 1
        )

    [1] => Array
        (
            [sp_id] => 2
            [specifications_name] => example 2
        )

    [2] => Array
        (
            [sp_id] => 3
            [specifications_name] => example 3
        )

    [3] => Array
        (
            [sp_id] => 4
            [specifications_name] => example 4
        )

    [4] => Array
        (
            [sp_id] => 5
            [specifications_name] => example 5
        )

    [5] => Array
        (
            [sp_id] => 6
            [specifications_name] => example 6
        )

)
    $getgeneral = explode(",",$info['developerprofile']['general_Specifications']); // getting below output

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
)

我必须显示数据库中的所有列表,并且我已根据获取表单数据库的第二个数组值选中了复选框。

我尝试了下面的代码,我得到了输出,但缺少一个值。 我的意思是,如果我有数组 1,2,3,4 那么我就会得到 1,2,3

    <?php 
    $specification=getgeneralSpecifications($pdo);
    $getgeneral = explode(",",$info['developerprofile']['general_Specifications']);

    foreach ($specification as $key=> $row) {
      $checked="";
      if(in_array($key, $getgeneral)){
        $checked="checked";
          }
      ?>
      <li><label><?php echo $row['specifications_name'];?></label>
        <div class="form-check">
          <input class="form-check-input custom-checkbox generalsinglecheck" type="checkbox" value="<?php echo $row['sp_id'];?>" name="general_specification[]" <?php echo $checked;?>>
        </div>
      </li>
    <?php } ?>

最佳答案

这是一个简单的错误行索引和 sp_id 值(也是数字)的问题。

您的 $key 变量更适合命名为 $index,但事实是您根本不需要声明该变量。相反,在与 $getgeneral 数组进行比较时引用行的 sp_id ,一切都会好起来的。


我建议创建一个干净的模板字符串以在迭代时使用。 printf() 非常适合这种技术。这样您就可以整齐地标记您的标记,并且不需要使用与内联条件 block 混合的困惑插值/串联。

哦,我将在 foreach() 中演示数组解构,但您不一定需要这样做 - 如果您愿意,您可以通过子数组值的键来访问子数组值.

代码:( Demo ) ( Demo without destructuring )

function getgeneralSpecifications() {
    return [
        ['sp_id' => 1, 'specifications_name' => 'example 1'],
        ['sp_id' => 2, 'specifications_name' => 'example 2'],
        ['sp_id' => 3, 'specifications_name' => 'example 3'],
        ['sp_id' => 4, 'specifications_name' => 'example 4'],
        ['sp_id' => 5, 'specifications_name' => 'example 5'],
        ['sp_id' => 6, 'specifications_name' => 'example 6'],
    ];
}

$checked = explode(',', '1,2,4');

echo "<ul>";
foreach (getgeneralSpecifications() as ['sp_id' => $id, 'specifications_name' => $name]) {
    printf(
        '<li>
            <label>%s</label>
            <div class="form-check">
                <input class="form-check-input custom-checkbox generalsinglecheck"
                       type="checkbox"
                       value="%d"
                       name="general_specification[]"
                       %s>
            </div>
        </li>',
        $name,
        $id,
        in_array($id, $checked) ? 'checked' : ''
    );
}
echo "</ul>";

输出:

<ul>
    <li>
        <label>example 1</label>
        <div class="form-check">
            <input class="form-check-input custom-checkbox generalsinglecheck"
                   type="checkbox"
                   value="1"
                   name="general_specification[]"
                   checked>
        </div>
    </li>
    <li>
        <label>example 2</label>
        <div class="form-check">
            <input class="form-check-input custom-checkbox generalsinglecheck"
                   type="checkbox"
                   value="2"
                   name="general_specification[]"
                   checked>
        </div>
    </li>
    <li>
        <label>example 3</label>
        <div class="form-check">
            <input class="form-check-input custom-checkbox generalsinglecheck"
                   type="checkbox"
                   value="3"
                   name="general_specification[]"
                   >
        </div>
    </li>
    <li>
        <label>example 4</label>
        <div class="form-check">
            <input class="form-check-input custom-checkbox generalsinglecheck"
                   type="checkbox"
                   value="4"
                   name="general_specification[]"
                   checked>
        </div>
    </li>
    <li>
        <label>example 5</label>
        <div class="form-check">
            <input class="form-check-input custom-checkbox generalsinglecheck"
                   type="checkbox"
                   value="5"
                   name="general_specification[]"
                   >
        </div>
    </li>
    <li>
        <label>example 6</label>
        <div class="form-check">
            <input class="form-check-input custom-checkbox generalsinglecheck"
                   type="checkbox"
                   value="6"
                   name="general_specification[]"
                   >
        </div>
    </li>
</ul>

关于php - 在 php 中使用 in_array 时缺少一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67956042/

相关文章:

php - Joomla com_user 扩展类似于 com_categories 和 com_content

javascript - 编写 WordPress 插件时没有错误不执行

php - 某列分组显示的SQL语句

jquery - 使用 Jquery 逐行选择表格内的所有复选框

java - NatTable - CheckBoxCellEditor 和 CheckBoxPainter 在单击时显示文本

javascript - 选中输入标签复选框时显示文本

php - 使用 mysqli_fetch_assoc 输出多个结果

javascript - 是否可以暂时卡住 HTML 元素的当前外观?

javascript - 在第一层创建 JSON 属性键链接

html - 在我的案例中如何为 li 元素创建边框?