Posted at

php5.4以降は連想配列の定義がラクになった

More than 5 years have passed since last update.

php5.4以降は連想配列の表記が少しラクになりましたよ。

"array"とタイプする必要がなくなったので、タイプ量が減ります。


array.php

<?

/**
* 古い連想配列表現方法を使った多重配列定義例
*
*/

$array_old = array(
array('index' => 1,
array('hoge' => 'hohoho', 'fuga' => 'fufufu'),
array('updated_at' => time()),
),
array('index' => 20,
array('hoge' => 'gegege', 'fuga' => 'gagaga'),
array('updated_at' => time()),
),
array('index' => 300,
array('hoge' => 'ohohoh', 'fuga' => 'ufufuf'),
array('updated_at' => time()),
),
array('index' => 4000,
array('hoge' => 'egegeg', 'fuga' => 'egegeg'),
array('updated_at' => time()),
),
);

/**
* php5.4から採用された新しい連想配列表現方法を使った多重配列定義例
*
*/

$array_new = [
['index' => 1,
['hoge' => 'hohoho', 'fuga' => 'fufufu'],
['updated_at' => time()],
],
['index' => 20,
['hoge' => 'gegege', 'fuga' => 'gagaga'],
['updated_at' => time()],
],
['index' => 300,
['hoge' => 'ohohoh', 'fuga' => 'ufufuf'],
['updated_at' => time()],
],
['index' => 4000,
['hoge' => 'egegeg', 'fuga' => 'egegeg'],
['updated_at' => time()],
],
];
}