PHP:在数组中存储变量的便捷方法是什么?


PHP: what is the convinient way to store variables in array?

我知道可以这样做

test['array'][0] = 'A';
test['array'][1] = 'B';
test['array'][2] = 'C';
test['array'][3] = 'D';

有没有比上面的例子更简单或更好的方法来在数组中存储变量?^_^

$test['array']=['A','B','C','D'];

默认方式(适用于所有PHP版本)

$test['array'] = array('A','B','C','D');

在 PHP 5.4 及更高版本中,您可以使用 JS 样式数组声明

$test['array'] = ['A','B','C','D'];
test['array'][] = 'A';
test['array'][] = 'B';
test['array'][] = 'C';
test['array'][] = 'D';

甚至更简单:

test[] = 'A';
test[] = 'B';
test[] = 'C';
test[] = 'D';

$test['array'] = ['A', 'B', 'C', 'D'];

array_push($test['array'], 'A', 'B', 'C', 'D');