Get a variable from object in array [closed]

I’m trying to echo get_the_category($id)[0]->slug and this works on localhost but not on the server.

The error is

Parse error: syntax error, unexpected ‘[‘….

Is there a workaround?

1 Answer
1

Probably your hosting server has a different and older version of php so this syntax is not supported.

The above syntax is implemented in PHP 5.4.

You have to do it like this:

$category = get_the_category($id);
echo $category[0]->slug;

Source:

https://bugs.php.net/bug.php?id=45906

Leave a Comment