Difference between myVar[0] and myVar->{0}?

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

Hi, I’m not that familiar with PHP to know what that code is meant to do, however it looks to me like curly braces are being used incorrectly…


$string = 'abc';
echo $string{1}; // b
echo $string{0}; // a

$array = array('a', 'b', 'c');
echo $array[1]; // b
echo $array[0]; // a

$obj = json_decode('{"a" : "a", "b" : "b", "c" : "c"}');
echo $obj->{a}; // Use of undefined constant a - assumed 'a'

Turn on error reporting to see what is happening:

error_reporting(E_ALL);
ini_set(‘display_errors’, ‘On’);

AFAIK, like the above poster I’m not too adept but {} creates a variable definition.

<?php

$obj = null;
$obj->{'eh'} = 'I AM CANADIAN';

print $obj->eh; // I AM CANADIAN

var_dump($obj);

/* var_dump is...
object(stdClass)#1 (1) {
  ["eh"]=>
  string(13) "I AM CANADIAN"
}
*/
?>