Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
What does $results->result() yield? – hjpotter92 Jan 31 at 2:54

marked as duplicate by mario, hjpotter92, Lightness Races in Orbit, NullPoiиteя, brenjt Jan 31 at 3:26

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer

up vote 5 down vote accepted

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();
?>
share|improve this answer
ah thank you very much – user1093111 Jan 31 at 3:26

Not the answer you're looking for? Browse other questions tagged or ask your own question.