doushan2311 2013-09-11 18:57
浏览 96
已采纳

PHP isset返回true但应该为false

My project is in cakePHP but I think this is an aspect of native PHP that I am misunderstanding..

I have an afterFind($results, $primary = false) callback method in my AppModel. On one particular find if I debug($results); I get an array like this

array(
    'id' => '2',
    'price' => '79.00',
    'setup_time' => '5',
    'cleanup_time' => '10',
    'duration' => '60',
    'capacity' => '1',
    'discontinued' => false,
    'service_category_id' => '11'
)

In my afterFind I have some code like this:

foreach($results as &$model) {
  // if multiple models
  if(isset($model[$this->name][0])) {
    ....

The results of the find are from my Service model so inserting that for $this->name and checking if(isset($model['Service'][0])) should return false but it returns true? if(isset($model['Service'])) returns false as expected.

I am getting the following PHP warning:

Illegal string offset 'Service'

so what's going on here? why does if(isset($model['Service'][0])) return true if if(isset($model['Service'])) returns false?

UPDATE:

I still don't know the answer to my original question but I got around it by first checking if $results is a multidimensional array with

if(count($results) != count($results, COUNT_RECURSIVE))

  • 写回答

2条回答 默认 最新

  • doutangguali32556 2013-09-11 19:12
    关注

    Use array_key_exists() or empty() instead of isset(). PHP caches old array values strangely. They have to be manually unset using unset()

    isset() does not return TRUE for array keys that correspond to a NULL value, while array_key_exists() does.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow