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 ?
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.
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'];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With