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))

有帮助吗?

解决方案

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.

其他提示

String offsets provide a mechanism to use strings as if they were an array of characters:

$string = 'abcde';
echo $string[2]; // c

$model is indeed a string for all keys except discontinued.

As for the isset($model['Service'][0]) return value, I'm a bit surprised. This is a simplified test case:

$model = '2';
var_dump(isset($model['Service'])); // bool(false)
var_dump(isset($model['Service'][0])); // bool(true)

There must be a reason somewhere. Will have a dig..

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top