I just took over a PHP project and I’m currently sorting through some issues in the code I inherited. In a function that returns search results from ASK, the “raw results” are returned by a call to simplexml_load_file($url). The code that process these results looks like this:
$this->process($this->raw_results->Engine->{0}->ResultSet,“sponsored”);
$this->process($this->raw_results->Engine->{1}->ResultSet,“results”);
etc.
This seems to work fine on the live server. However, the code doesn’t work on my local Apache server. When I tried stepping through with Zend debugger, I noticed that ‘Engine’ is actually an array, and putting the two expressions above in my watch list yields ‘null’ or undefined values. However, if I change the code to:
$this->process($this->raw_results->Engine[0]->ResultSet,"sponsored");
$this->process($this->raw_results->Engine[1]->ResultSet,"results");
I can see valid data in these expressions. However, the code still doesn’t work. If I do the following:
$this->process($this->raw_results->Engine[0]->ResultSet,"sponsored");
$this->process($this->raw_results->Engine->{1}->ResultSet,"results");
The code works. So there is obviously some side-effect or nuance in “process()” which I need to deal with.
My question is, what are the differences between the two ([0] and ->{0})? As far as I can tell, the latter is a usage of PHP’s complex curly syntax, but it still doesn’t quite make sense to me. Is using curly brackets a valid way of accessing an array, and does it yield any differences than using square brackets?
Thx,
Helo