Cakephp throws an error unless array is devided

限于喜欢 提交于 2020-01-06 15:06:59

问题


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?


回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/18469032/cakephp-throws-an-error-unless-array-is-devided

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!