dqstti8945 2016-07-19 14:09
浏览 96
已采纳

为多维数组中的特定键生成复选框

I have 2 multidimensional arrays $investmentProgramExistingCriteriaoutput and $criteria which when printed (print_r) produces the following output:

Array $investmentProgramExistingCriteriaoutput:

Array
(
[0] => Array
    (
        [key1] => 1
        [key2] => 4
        [criteriaID] => 25
    )

[1] => Array
    (
        [key1] => 2
        [key2] => 4
        [criteriaID] => 26
    )

)

Array $criteria output:

Array
(
[0] => Array
    (
        [criteriaID] => 27
        [key3] => 1
        [key4] => Some value
        [key5] => Yes
        [key6] => 3
        [key7] => 1
    )

[1] => Array
    (
        [criteriaID] => 25
        [key3] => 5
        [key4] => Some other value
        [key5] => 1, 2, 3
        [key6] => 1
        [key7] => 1
    )

[2] => Array
    (
        [criteriaID] => 26
        [key3] => 1
        [key4] => Some different value
        [key5] => Ναί
        [key6] => 1
        [key7] => 1
    )

)

I am trying to generate check boxes for each item in $criteria array. However, if the criteriaID in the $criteria array exists in the $investmentProgramExistingCriteriaoutput array, the checkbox should be checked, if not, it should be unchecked.

I am trying to do that with the following code:

foreach ($criteria as $val) {
    foreach ($investmentProgramExistingCriteria as $existingcriteria) {
        if($val['criteriaID'] == $existingcriteria['criteriaID']) {
            echo "<input type='checkbox' name='criteria[]' value=".$val['criteriaID']." style='margin-bottom:20px;float:left;' checked='checked' />&nbsp;&nbsp;&nbsp;";
            echo "<span style='line-height:20px;'>".$val['criteriaDescription'] ."</span><br /><br />";  
        } else {
            echo "<input type='checkbox' name='criteria[]' value=".$val['criteriaID']." style='margin-bottom:20px;float:left;' />&nbsp;&nbsp;&nbsp;";
            echo "<span style='line-height:20px;'>".$val['criteriaDescription'] ."</span><br /><br />";   
        }
    }
}

As you can see from the above array values, I should get 3 checkboxes, from which 2 should be checked. However this code print 6 checkboxes instead of 3,

2 of the 6 printed checkboxes them are checked (correctly). How can I get rid of duplicate checkboxes?

  • 写回答

2条回答 默认 最新

  • doufeng9567 2016-07-19 14:23
    关注

    Rather than nested loops you might try iterating through the first array ( renamed here for brevity ) and assigning criteriaID into a new array. Then iterate through the criteria array and see if the value exists.

    $ipec=array(
        array('k1'=>1,'k2'=>4,'criteriaID'=>25),
        array('k1'=>2,'k2'=>4,'criteriaID'=>26)
    );
    $criteria=array(
        array('criteriaID'=>27,'k3'=>1,'k4'=>4),
        array('criteriaID'=>25,'k3'=>1,'k4'=>4),
        array('criteriaID'=>26,'k3'=>1,'k4'=>4)
    );
    
    $cids=array();
    foreach($ipec as $a)$cids[]=$a['criteriaID'];
    
    
    foreach($criteria as $a){
        $c=$a['criteriaID'];
        $checked = in_array( $c, $cids ) ? 'checked=true' : '';
    
        /* There is no key "criteriaDescription" in the quoted arrays */
        echo "
            <input type='checkbox' name='criteria[]' value='{$c}' style='margin-bottom:20px;float:left;' {$checked} />
            &nbsp;&nbsp;&nbsp;
            <span style='line-height:20px;'>{$val['criteriaDescription']}</span>
            <br />
            <br />";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 数据量少可以用MK趋势分析吗
  • ¥15 使用VH6501干扰RTR位,CANoe上显示的错误帧不足32个就进入bus off快慢恢复,为什么?
  • ¥15 大智慧怎么编写一个选股程序
  • ¥100 python 调用 cgps 命令获取 实时位置信息
  • ¥15 两台交换机分别是trunk接口和access接口为何无法通信,通信过程是如何?
  • ¥15 C语言使用vscode编码错误
  • ¥15 用KSV5转成本时,如何不生成那笔中间凭证
  • ¥20 ensp怎么配置让PC1和PC2通讯上
  • ¥50 有没有适合匹配类似图中的运动规律的图像处理算法
  • ¥15 dnat基础问题,本机发出,别人返回的包,不能命中