3

Is this valid PHP syntax or just a shorthand for the documentation? Does anybody have the link to a help page describing this notation?

$array = [ [1, 2], [3, 4], ]; 

If you have a look at the code examples in the Unpacking nested arrays with list() section on the foreach help page you will see this possibly new notation, however I can't seem to find any help for this.

8
  • 6
    If you'd try running the code you would quickly find out if it works or not. Commented Jun 26, 2013 at 14:36
  • 1
    @Jessica I haven't got 5.5 installed - have you??? Commented Jun 26, 2013 at 14:37
  • its just short hand for an array of arrays i believe Commented Jun 26, 2013 at 14:37
  • 5
    "I haven't got 5.5 installed" - 3v4l.org/VNp6s Commented Jun 26, 2013 at 14:39
  • 3
    It was a lazy question, why do you expect non-lazy answers? Commented Jun 26, 2013 at 14:41

4 Answers 4

14

The new syntax [] was added in PHP 5.4.0. Check out the documentation on arrays

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

1 Comment

"As of PHP 5.4 you can also use the short array syntax, which replaces array() with []."
4

The page you reference is saying that unpacking the array using list() is new in PHP 5.5, not the short array syntax itself, which was added in PHP 5.4.

PHP 5.5 added the ability to iterate over an array of arrays and unpack the nested array into loop variables by providing a list() as the value.

Comments

2

That is no more than an array of arrays. These two snippets are identical:

$array = [ [1, 2], [3, 4], ]; $array = array( array (1, 2), array (3, 4), ); 

This is documented in the PHP manual page for Arrays and has been possible since PHP 5.4.

1 Comment

I know what it means. I am asking if it is valid PHP now & do you have a link to the documentation?
1

Why don't you just test the code? I'm using PHP Version 5.4.7 and it is already valid code. Example:

$array = [ [1, 2], [3, 4], ]; echo '<pre>'; print_r($array); echo '</pre>'; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.