douhuilin1152 2014-07-02 11:20
浏览 37
已采纳

在2个数组内循环

I have 2 arrays, and I am trying to find out which checkboxes should be checked and which not. I do get correct results, but if 2 checkboxes should be checked, I get 2 sets of checkboxes, if 3 should be checked i get 3 set of checkboxes and so on... I make a numerous attempts to avoid that using break and continue statements, without success.

Here is the code:

<?php
foreach ($rooms as $room) {
    foreach ($searchQuerySelectedRooms as $selRoom) {
        if ($room['roomID'] != $selRoom) {
?>
<input type="checkbox" name="rooms[]" value="<?php echo $room['roomID']; ?>" style="float:left;width:20px; margin-right:10px;"/>
<?php
        } else {
?>
<input type="checkbox" name="rooms[]" value="<?php echo $room['roomID']; ?>" style="float:left;width:20px; margin-right:10px;" checked="checked"/>
<?php                                                                  
        } 
    }
}
?>

Array $rooms prints the following:

Array
(
[0] => Array
    (
        [roomID] => 2
        [hotelID] => 10
        [roomtypeID] => 1
        [roomNumber] => 1
    )

[1] => Array
    (
        [roomID] => 3
        [hotelID] => 10
        [roomtypeID] => 1
        [roomNumber] => 2
    )

[2] => Array
    (
        [roomID] => 4
        [hotelID] => 10
        [roomtypeID] => 2
        [roomNumber] => 3
    )
[3] => Array
    (
        [roomID] => 5
        [hotelID] => 10
        [roomtypeID] => 2
        [roomNumber] => 4
    )
)

While array $searchQuerySelectedRooms prints the following:

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

Any help will be deeply appreciated.

Regards, John

  • 写回答

2条回答 默认 最新

  • douzha8489 2014-07-02 11:39
    关注

    please check this code

    <?php
    foreach ($rooms as $room) {
        if (in_array($room['roomID'], $searchQuerySelectedRooms)) {
            $checker = 'checked="checked"';
        } else {
            $checker = '';
        }
    
        echo '<input type="checkbox" name="rooms[]" value="' . $room['roomID'] . '" style="float:left;width:20px; margin-right:10px;" ' . $checker . '/>';
    
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了