4

I am using this code below :

$data = array();
$value = reset($value);
$data[0] = (string) $value->attributes()['data'];
------^

I have no problem in localhost, but in other host, when i check the code, i see this error :

Parse error: syntax error, unexpected '[' in ....

I have shown where the code causes error .

i have also used :

$data[] = (string) $value->attributes()['data'];

(without 0 in [])

How can i solve it ?

Cab
  • 121
  • 1
  • 1
  • 12
  • 7
    The issue is not with the first square brackets, it's with the ones after `attributes()`. –  Jun 04 '14 at 11:22
  • 5
    It's got nout to do with that first `[`, it's your function array dereferencing (the last `[]`). You need PHP >= 5.4. – Prisoner Jun 04 '14 at 11:22

2 Answers2

8

Array Referencing was first added in PHP 5.4.

The code from PHP.net:

<?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();
?>

So you'd have to change

$data[] = (string)$value->attributes()['data'];

to

$attributes = $value->attributes();
$data[] = (string)$attributes['data'];

If your PHP version is older than 5.4.

h2ooooooo
  • 39,111
  • 8
  • 68
  • 102
5

The problem is this line:

$value->attributes()['data'];

Which is because you're using a version of PHP which doesn't support function array dereferencing, which was only added in PHP 5.4

To get around it, you'd have to call the method first, and then access its properties, eg:

$someVariable = $value->attributes();
$data[] = (string) $someVariable['data'];
billyonecan
  • 20,090
  • 8
  • 42
  • 64