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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题