donglanying3855 2012-04-09 17:21
浏览 27
已采纳

如何在PHP中区分两个多维数组?

I'm coding an edit form and I can't get the part where you check previously checked checkboxes to function properly.

For example I have a list of five items in a database, I previously checked two of them and saved the form. When I press edit on the form, I need to get this again:

[ ] Item 1
[X] Item 2
[ ] Item 3
[X] Item 4
[ ] Item 5

I have two arrays:

  • $amenities All items in the database are here.

  • $related All previously checked items are here. (in this case, two of them)

How can I walk the two arrays comparing them so if an item in $related is found in $amenities, it will print the box checked and if not, it will print it unchecked. This is my code relevant to that part.

$amenities (print_r)

  Array
(
    [0] => Array
        (
            [itemID] => 3
            [itemName] => Crema Chantilly
        )

    [1] => Array
        (
            [itemID] => 4
            [itemName] => Caribe Cooler
        )

    [2] => Array
        (
            [itemID] => 5
            [itemName] => Cacahuates Japoneses
        )

    [3] => Array
        (
            [itemID] => 6
            [itemName] => Cerveza Sol (lata)
        )

    [4] => Array
        (
            [itemID] => 7
            [itemName] => Chocolate derretido
        )

)

$related (print_r)

  Array
(
    [0] => Array
        (
            [itemID] => 3
            [itemName] => Crema Chantilly
        )

    [1] => Array
        (
            [itemID] => 4
            [itemName] => Caribe Cooler
        )

)

As you can see, there are two items in $related that match two items in $amenities.

What I'm trying here is something like this:

<?php foreach ($related as $single) : ?>

    <?php foreach ($amenities as $amenity) : ?>

      <?php if ( $single === $amenity) : ?>

        <input type="checkbox" class="left" checked="yes" name="amenities[]" value="<?=$amenity['itemID']?>">
        <label class="checkbox"><?=$amenity['itemName']?></label>

       <?php else : ?>

        <input type="checkbox" class="left" name="amenities[]" value="<?=$amenity['itemID']?>">
        <label class="checkbox"><?=$amenity['itemName']?></label>

      <?php endif ?>

    <?php endforeach;?>

<?php endforeach;?>

But, the results are being duplicated for the full options list. I'm getting this:

[X] Item 1
[ ] Item 2
[ ] Item 3
[ ] Item 4
[ ] Item 5
[ ] Item 1
[ ] Item 2
[X] Item 3
[ ] Item 4
[ ] Item 5

And I need to get

[X] Item 1
[ ] Item 2
[X] Item 3
[ ] Item 4
[ ] Item 5

Maybe it's something easy to do but I can't figure out how to get this done. Maybe I am taking the wrong approach with the two foreach?

  • 写回答

2条回答 默认 最新

  • dongyong6045 2012-04-09 17:28
    关注

    Instead of writing in every loop check its existance and set its status checked

    <?php foreach ($amenities as $single) : ?>
    <?php $strChecked = '';?>
        <?php foreach ($related as $amenity) : ?>
    
          <?php if ( $single === $amenity) : ?>
              <?php $strChecked = ' checked="checked"';break;?>
    
          <?php endif ?>
    
        <?php endforeach;?>
    
            <input type="checkbox" class="left" <?php echo $strChecked;?> name="amenities[]" value="<?=$amenity['itemID']?>">
            <label class="checkbox"><?=$amenity['itemName']?></label>
    <?php endforeach;?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug