Marc Rasmussen
Marc Rasmussen

Reputation: 20555

Cakephp throws an error unless array is devided

So i have an Api call where i get a json array:

When i do the following:

$data = $this->HasOffers->get_full_detail_report()['data']['data'];
$this->set('data',$data);

i get an error saying an internal error has occoured

However if i do:

$data = $this->HasOffers->get_full_detail_report();
$data2 = $data['data']['data'];
$this->set('data',$data2);

everything is working correctly.

Now my question is why is this happening? and how can i fix it?

Upvotes: 0

Views: 114

Answers (3)

Nunser
Nunser

Reputation: 4522

I'm guessing you have debug < 2, so the description of the error is not very detailed. However, that behaviour is known to be a PHP < 5.4 issue (post regarding that subject).

To "fix" it, you need to upgrade PHP to 5.4 at least. Or, just use an intermediary variable for those cases, it's not that bad.

Upvotes: 2

keithhatfield
keithhatfield

Reputation: 3273

The syntax you are using in the first example is only available in PHP >= 5.4. See relevant section of PHP manual: http://php.net/manual/en/language.types.array.php#example-88

You can see an example running in different versions of PHP at: http://3v4l.org/XhCKH

Your CakePHP site likely has error reporting turned off so, rather than displaying the syntax error, it is displaying an Internal Error.

Upvotes: 2

J.Michieli
J.Michieli

Reputation: 91

This is happening because the array you are referencing in the first example only exists after the function get_full_detail_report() is called. PHP does not like this. PHP wants your array to exists before you reference it. I assume that it attempts to locate any variables within your statement before performing any operations, which would mean it is searching for an array that does not exist until it performs those operations.

If anyone has any more insight into this, I would welcome their revisions / comments.

Upvotes: 0

Related Questions