【发布时间】:2013-11-02 14:19:36
【问题描述】:
我知道可以这样做
test['array'][0] = 'A';
test['array'][1] = 'B';
test['array'][2] = 'C';
test['array'][3] = 'D';
有没有比上面的例子更简单或更好的方法将变量存储在数组中? ^_^
【问题讨论】:
-
做一些谷歌或者有PHP手册,这是非常基本的,可以在手册中轻松获得。
我知道可以这样做
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');
【讨论】: