繁体   English   中英

如何在PHP中检查多维数组

[英]How check multidimensional array in php

如果返回值匹配,如何检查复选框,否则取消选中复选框。

返回数组:

Array (
    [0] => Array (
        [Property_Name] => LMS Unlink Customer
    )
    [1] => Array (
        [Property_Name] => LMS Notification
    )
)
<th style="text-align: center;"><input type="checkbox" class="flat" <?php if($getlmsprivileges['Property_Name'] == 'LMS Reward'){ ?> checked <?php } ?> name="Reward"></th>
    <td style="text-align: center;"><input type="checkbox" class="flat" <?php if($getlmsprivileges['Property_Name'] == 'LMS Promotion'){ ?> checked <?php } ?> name="Promotion"></td>
    <td style="text-align: center;"><input type="checkbox" class="flat" <?php if($getlmsprivileges['Property_Name'] == 'LMS VAS'){ ?> checked <?php } ?> name="VAS"></td>                          
    <td style="text-align: center;"><input type="checkbox" class="flat" <?php if($getlmsprivileges['Property_Name'] == 'LMS Unlink Customer'){ ?> checked <?php } ?> name="Unlink_Customer"></td>                          
    <td style="text-align: center;"><input type="checkbox" class="flat"  <?php if($getlmsprivileges['Property_Name'] == 'LMS Notification'){ ?> checked <?php } ?> name="Notification"></td>                                                    

PHP功能

function Property_Name_Check($data,$check){
  foreach ($data as $key => $value) {
    if($value['Property_Name'] == $check){
      return true;
    }
  }
return false;
}

HTML元素

<th style="text-align: center;"><input type="checkbox" class="flat" name="Reward" <?php echo (Property_Name_Check($getlmsprivileges,'LMS Reward')) ? 'checked="true"' : ''; ?>></th>
<td style="text-align: center;"><input type="checkbox" class="flat" name="Promotion" <?php echo (Property_Name_Check($getlmsprivileges,'LMS Promotion')) ? 'checked="true"' : ''; ?>></td>
<td style="text-align: center;"><input type="checkbox" class="flat" name="VAS" <?php echo (Property_Name_Check($getlmsprivileges,'LMS VAS')) ? 'checked="true"' : ''; ?>></td>
<td style="text-align: center;"><input type="checkbox" class="flat" name="Unlink_Customer" <?php echo (Property_Name_Check($getlmsprivileges,'LMS Unlink Customer')) ? 'checked="true"' : ''; ?>></td>
<td style="text-align: center;"><input type="checkbox" class="flat" name="Notification" <?php echo (Property_Name_Check($getlmsprivileges,'LMS Notification')) ? 'checked="true"' : ''; ?>></td>

由于要从数据库中检索此数组,因此您需要将数组中每一行的值映射到表单中的相应复选框。 这种确定是否选中chexkbox的方法只是布尔操作。 数组中不存在该值的情况表示未选中该值,而存在值则确定该值处于选中状态。

现在看来,您存储带有某些前缀的复选框名称,作为从数据库中获取的值。 因此,您需要做的是找到一种方法,以获取要在表单中打印出的所有预期复选框名称,并将它们从数据库中获取的值映射为这样的数组...

$allCheckboxNames = [
    "Reward"          => false,
    "Promotion"       => false,
    "VAS"             => false,
    "Unlink_Customer" => false,
    "Notification"    => false,
];

因此,假设您在数据库结果中"Unlink_Customer""Unlink_Customer""Notification"

$resultArray = [
    ["Property_Name" => "Unlink_Customer"],
    ["Property_Name" => "Notification"],
];

我们将创建两者的一个交集和一个并集,以将所有元素的布尔值表示为选中或未选中。

但是首先,您必须像这样将二维数组转换为平面一维数组。

$resultArray = array_map(function($n) { return $n["Property_Name"]; }, $resultArray);

// Since we also need the values as keys we'll flip the array
$resultArray = array_map(function($v) { return true; }, array_flip($resultArray));

现在,我们只需要通过键相交并合并所有键即可。

$resultArray = array_intersect_key($resultArray, $allCheckboxNames) + $allCheckboxNames;

现在从该数组产生输出变得无关紧要。 您只需使用数组的key作为复选框名称,并使用数组的布尔value作为是否被检查的描述,就可以将它们全部循环打印出来。

<?php
foreach($resultArray as $name => $checked) {
  ?>
  <input type="checkbox" class="flat" <?=$checked ? 'checked' : null; ?> name="<?=$name?>">
  <?php
}

这是MVCE的完整实验室

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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