1

I got used to this notation for creating empty arrays and add named elements to them when needed;

$array = [];

// in case there is an error
$array["error"][] = "new error message as element 0 of $array['error']";

Now I learned that the [] notation for arrays does not work in older versions of PHP, like PHP 5.2.

Instead I have to do;

$array = array(
  "error" => array()
);

array_push($array["error"], "new error message as element 0 of $array['error']");

This way is a little bit inconvenient in my case because the great thing about the first code snippet is that the "error" entry in $array is only created when there is an actual error, whereas in the latter case the entry (although empty) exists either way.

Is there a way to get similar 'functionality' (i.e. specifying/adding named elements when needed, not at initialisation) in a way that is also easily readable in PHP 5.2?

grueb
  • 123
  • 11
  • 3
    "Now I learned that the [] notation for arrays does not work in older versions of PHP, like PHP 5.2." not correct –  Aug 20 '15 at 01:33
  • I'm voting to close this question as off-topic because its base assertion is incorrect –  Aug 20 '15 at 01:35
  • hm, I ran into problems with [] in 5.2 and found this entry that made sense to me http://stackoverflow.com/a/5966855/3097469 – grueb Aug 20 '15 at 01:36
  • 2
    @grueb You might be confusing `$array = []`, which declares an array and is not supported in older versions of php with `$array[] = $something`, which appends an element to the array and is supported in all versions of php – FuzzyTree Aug 20 '15 at 01:37
  • 2
    The [short array syntax](http://php.net/manual/en/language.types.array.php) (`$array = []`) was introduced on PHP 5.4 – axiac Aug 20 '15 at 01:40
  • @ FuzzyTree, yes sorry – that was a just a pasting error – that is exactly what I used ... corrected it in the post. Thank you for clarifying! – grueb Aug 20 '15 at 01:41

2 Answers2

3

EDIT: The first code snippet in the original post was reading $array = array[];. The author corrected it after I posted this answer.


The first code snipped is incorrect. There is no such thing as array[]. The correct syntax is array().

$array = array();

// in case there is an error
$array["error"][] = "new error message as element 0 of $array['error']";

You don't have to worry about PHP versions. This syntax always worked on PHP since its dawn and it will probably work forever. Keep using it.

axiac
  • 68,258
  • 9
  • 99
  • 134
  • great, thank you! I had `$array = [];` in my original code ... but got confused / made a pasting error (corrected it in the original post) ... but now I understand that this was the problem and not `$array["error"][] = ...`. Thank You! – grueb Aug 20 '15 at 01:43
1

The first way of creating array in PHP is incorrect. This syntax works in PHP5.2 below too, so you dont need to worry about it. You don't need to use array_push and simply do following.

The correct syntax is:

$array = array(); // notice it doesn't to array[]

// add error when there is one
$array["error"][] = "new error message as element 0 of $array['error']";
Subash
  • 7,098
  • 7
  • 44
  • 70