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 chaquopy python 安卓
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题