PHP isset返回true,但应该是false
我的项目在cakePHP中,但我认为这是我误解的本地PHP的一个方面。
  我有一个afterFind($results, $primary = false)回调在我的方法AppModel 。  在一个特定的发现,如果我debug($results);  我得到这样的数组 
array(
    'id' => '2',
    'price' => '79.00',
    'setup_time' => '5',
    'cleanup_time' => '10',
    'duration' => '60',
    'capacity' => '1',
    'discontinued' => false,
    'service_category_id' => '11'
)
  在我的afterFind我有这样的代码: 
foreach($results as &$model) {
  // if multiple models
  if(isset($model[$this->name][0])) {
    ....
  查找的结果来自我的Service模型,因此插入$this->name并检查if(isset($model['Service'][0]))应该返回false,但它返回true?  if(isset($model['Service']))按预期返回false。 
我收到以下PHP警告:
非法字符串偏移'服务'
  所以这里发生了什么?  为什么if(isset($model['Service'][0]))返回true,如果if(isset($model['Service']))返回false? 
更新:
我仍然不知道我原来的问题的答案,但我通过首先检查$ results是否是一个多维数组来解决它
 if(count($results) != count($results, COUNT_RECURSIVE)) 
  使用array_key_exists()或empty()代替isset() 。  PHP奇怪地缓存了旧的数组值。  他们必须使用unset()手动取消unset() 
对于对应于NULL值的数组键,isset()不返回TRUE,而array_key_exists()则返回TRUE。
字符串偏移提供了一种使用字符串的机制,就像它们是一个字符数组一样:
$string = 'abcde';
echo $string[2]; // c
  $model确实是除了停产之外的所有密钥的字符串。 
  至于isset($model['Service'][0])返回值,我有点惊讶。  这是一个简化的测试用例: 
$model = '2';
var_dump(isset($model['Service'])); // bool(false)
var_dump(isset($model['Service'][0])); // bool(true)
某处有一定的理由。 将有一个挖掘..
链接地址: http://www.djcxy.com/p/26617.html