8

In other languages such as C# and JavaScript, I am able to access the index of an array with a function call such as

getMyArray()[0] 

This would allow me to access the first index of the result instead of passing back the entire array and then setting the result.

However this shortcut does not work with PHP. Is there a way to get this shortcut?

2 Answers 2

15

You need to be running PHP 5.4 in order to use array de-referencing.

Sign up to request clarification or add additional context in comments.

2 Comments

Also, here's the new features list for migrating to 5.4.
So there is no support for this in 5.3.8?
3
// PHP 5.4
$item = getMyArray()[0];

// Older than 5.4: (not recommended)
list($a)   = getMyArray();  // getMyArray()[0]
list(, $b) = getMyArray();  // getMyArray()[1]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.