在 PHP 中,要向数组中添加一个值,可以使用以下方式:
array_push()
函数:$fruits = ['apple', 'banana', 'orange'];
array_push($fruits, 'grape');
$fruits = ['apple', 'banana', 'orange'];
$fruits[] = 'grape';
$fruits = ['a' => 'apple', 'b' => 'banana'];
$fruits['c'] = 'cherry';
无论使用哪种方法,添加值后,数组将会包含新的值。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:php数组增加一个值的方法是什么