0

Possible Duplicate:
PHP syntax for dereferencing function result

i wrote some code in php 5.4 and my server can only run 5.3,thus i have some syntax errors i need to fix.

when i got to the site I get this error

 Parse error: syntax error, unexpected '[' in /home/content/51/6663351/html/application/controllers/admin.php on line 247

line 247 is

if(count($results->result()) > 0)
            {
 this here>>>   $data['data'] = $results->result()[0];
                $data['cats'] = $this->db->get('category')->result();
                $data['curCat'] = $this->db->get('products_categories',     array('product_id' => $id))->result()[0];   

so I tried changing the code to:

 $data = array();
 if(count($results->result()) > 0)
            {
                $data['data'] = $results->result()[0];
                $data['cats'] = $this->db->get('category')->result();
                $data['curCat'] = $this->db->get('products_categories', array('product_id' => $id))->result()[0];

how ever adding the $data = array(); didn't fix anything. Does anyone have an idea what is wrong?

Community
  • 1
  • 1
user1093111
  • 1,091
  • 5
  • 21
  • 47

1 Answers1

5

This $results->result()[0] is called array dereferencing. It's new in PHP 5.4 so you can't do it in 5.3. You'll need to return that array element first and then use it in your code.

From the manual:

<?php
function getArray() {
    return array(1, 2, 3);
}

// on PHP 5.4
$secondElement = getArray()[1];

// previously
$tmp = getArray();
$secondElement = $tmp[1];

// or
list(, $secondElement) = getArray();
?>
John Conde
  • 217,595
  • 99
  • 455
  • 496