【问题标题】:How check multidimensional array in php如何在php中检查多维数组
【发布时间】:2016-09-04 05:44:16
【问题描述】:

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

返回数组:

大批 ( [0] => 数组 ( [Property_Name] => LMS 取消链接客户 ) [1] => 数组 ( [Property_Name] => LMS 通知 ) )
<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>                                                    

【问题讨论】:

  • 您显示的代码中没有LMS Notification
  • 您的复选框名称是单一的..那么您如何期望数组具有键“Property_Name”?这几乎没有任何意义。
  • 一开始你忽略了解释你是如何得到这个数组的。您期望此代码执行的操作与您的数组结构之间存在脱节。所以问题一定在于数组是如何生成的。
  • 我在 yii queryAll 结果的返回值上得到了这个数组格式

标签: php html arrays multidimensional-array


【解决方案1】:

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>

【讨论】:

    【解决方案2】:

    由于您要从数据库中检索此数组,因此您需要将数组中每一行的值映射到表单中相应的复选框。这种确定复选框是否被选中的方式只是一个布尔运算。数组中不存在值决定它未被选中,而存在值决定它被选中。

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

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

    假设在您的数据库结果中,"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
    }
    

    Here's a complete lab of the MVCE

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-05
      • 1970-01-01
      • 2023-03-26
      • 2011-06-24
      • 1970-01-01
      • 1970-01-01
      • 2019-04-01
      • 1970-01-01
      相关资源
      最近更新 更多