96

What is the shorthand for array notation in PHP?

I tried to use (doesn't work):

$list = {};

It will be perfect, if you give links on some information about other shorthands for PHP.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
James
  • 42,081
  • 53
  • 136
  • 161
  • There are many functions that can be used to create arrays in special cases (e.g., str_split), but I assume that's not what you are talking about. – Matthew Nov 24 '10 at 21:57
  • PHP hasn't. But phpreboot and pihipi provide experimental new syntax. – mario Nov 24 '10 at 21:58

8 Answers8

147

Update:
As of PHP 5.4.0 a shortened syntax for declaring arrays has been introduced:

$list = [];

Previous Answer:

There isn't. Only $list = array(); But you can just start adding elements.

<?php
$list[] = 1;
$list['myKey'] = 2;
$list[42] = 3;

It's perfectly OK as far as PHP is concerned. You won't even get a E_NOTICE for undefined variables.

E_NOTICE level error is issued in case of working with uninitialized variables, however not in the case of appending elements to the uninitialized array.

As for shorthand methods, there are lots scattered all over. If you want to find them just read The Manual.

Some examples, just for your amusement:

  1. $arr[] shorthand for array_push.
  2. The foreach construct
  3. echo $string1, $string2, $string3;
  4. Array concatenation with +
  5. The existence of elseif
  6. Variable embedding in strings, $name = 'Jack'; echo "Hello $name";
reikyoushin
  • 1,993
  • 2
  • 24
  • 40
Alin Purcaru
  • 43,655
  • 12
  • 77
  • 90
48

YES, it exists!!

Extracted from another Stack Overflow question:

The shortened syntax for arrays has been rediscussed, accepted, and is now on the way be released with PHP 5.4

Usage:

$list = [];

Reference: PHP 5.4 Short Hand for Arrays

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
AgelessEssence
  • 6,395
  • 5
  • 33
  • 37
37

It is also possible to define content inside [ ] like so:

  $array = ['vaue1', 'value2', 'key3'=>['value3', 'value4']];

This will only work in php5.4 and above.

Dieter Gribnitz
  • 5,062
  • 2
  • 41
  • 38
  • 2
    I think it is better to write 5.4 and above (in 5.6 it works). As a note it is the only working way to declare an array as a class constant (e.g. const x = ["a", "b"];) because the const x=array(....) doesn't work – Pierpaolo Cira Nov 23 '16 at 16:53
5

There are none as of PHP 5.3.

http://us.php.net/manual/en/language.types.array.php

Matthew
  • 47,584
  • 11
  • 86
  • 98
4

You can declare your array as follows:

$myArray1 = array(num1, num2, num3);
$myArray2 = array('string1', 'string2', 'string3');
$myArray3 = array( 'stringkey1'=>'stringvalue1', 'stringkey2'=>'stringvalue2');
$myArray4 = array( 'stringkey1'=>numValue1, 'stringkey2'=>numValue2);
$myArray5 = array( numkey1=>'stringvalue1', numkey2=>'stringvalue2');
$myArray6 = array( numkey1=>numValue1, numkey2=>numValue2);

You can have as many embedded arrays as you need.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
3

The only way to define an array in php is by the array() language construct. PHP doesn't have a shorthand for array literals like some other languages do.

Daniel Vandersluis
  • 91,582
  • 23
  • 169
  • 153
3

Nope, it was proposed and rejected by the community, so for now only syntax for arrays is array().

P.S. This is the old answer, now there is a syntax, check out other answers.

StasM
  • 10,593
  • 6
  • 56
  • 103
1

I just explode strings into an array like so:

$array = explode(",","0,1,2,3,4,5,6,7,8,9,10");
Damien Golding
  • 1,003
  • 4
  • 15
  • 28