guagay_wk
guagay_wk

Reputation: 28030

What is the right way to initialize a php associative array?

I am going to construct a highly complex associative array in php. But first, I need to initialize it.

What is the right way to initialize it? My initialization is as follows;

$ComplexAssociativeArray = [];

Are there better ways?

Upvotes: 1

Views: 891

Answers (1)

Mithun Satheesh
Mithun Satheesh

Reputation: 27845

If you go for

$array = [
    "foo" => "bar",
    "bar" => "foo",
];

It wont work in php versions before 5.4

but below way work in all versions

$array = array(
    "foo" => "bar",
    "bar" => "foo",
);

I think that is the main difference.

Quoting from php docs for arrays

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

Upvotes: 3

Related Questions