dourangdz750379 2017-01-18 11:01
浏览 42
已采纳

如何检查另一个数组中是否存在该数组键?

I'm using the CakePHP 2.0 framework and I'm facing one problem. It's giving me the following error. I also used array_key_exists.

: Undefined offset: 2 [APP/View/Doctors/admin_customize_plan_new.ctp, line 28]style="display:none"; id="checktab4">

I have two arrays. First is stored in $carePlansList:

Array
(
[0] => Array
    (
        [EveCarePlansList] => Array
            (
                [id] => 1
                [care_plan_name] => CHF
            )

    )

[1] => Array
    (
        [EveCarePlansList] => Array
            (
                [id] => 3
                [care_plan_name] => Hypertension
            )

    )

[2] => Array
    (
        [EveCarePlansList] => Array
            (
                [id] => 4
                [care_plan_name] => ABC
            )

    )
)

Second one is called $QuestionUserexists

Array
(
[0] => Array
    (
        [EveChfQuestionsUser] => Array
            (
                [cq_cp_id] => 1
            )

    )

[1] => Array
    (
        [EveChfQuestionsUser] => Array
            (
                [cq_cp_id] => 3
            )

    )

)

Now when I make a comparison between these two arrays, I'll get the error above.

My foreach loop is:

<?php 
foreach ($carePlansList as $key => $plansList) { ?>
    <li role="presentation" class="planLists" 
    <?php if($QuestionUserexists[$key]['EveChfQuestionsUser']['cq_cp_id'] == $plansList['EveCarePlansList']['id'] ){ 
        }else{?>
                style="display:none";
            <?php } ?>>
    </li>
  • 写回答

1条回答 默认 最新

  • dongshuofu0039 2017-01-18 11:07
    关注

    add a check: isset()

    foreach ($carePlansList as $key => $plansList) { ?>
    <li role="presentation" class="planLists" 
    <?php if(isset($QuestionUserexists[$key]) && 
    $QuestionUserexists[$key]['EveChfQuestionsUser']['cq_cp_id'] == $plansList['EveCarePlansList']['id'] ){ 
        }else{?>
                style="display:none";
            <?php } ?>>
    </li>
    endforeach;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作