0

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
  • What version of PHP are you using? IIRC, the function()['key'] syntax is only valid in PHP >= 5.4 Commented Aug 27, 2013 at 15:06

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.