php数组怎么增加一个值

lewis 2016-10-26 19次阅读

在 PHP 中,要向数组中添加一个值,可以使用以下方式:

  1. 使用 array_push() 函数:
$fruits = ['apple', 'banana', 'orange'];
array_push($fruits, 'grape');
  1. 直接给数组赋值:
$fruits = ['apple', 'banana', 'orange'];
$fruits[] = 'grape';
  1. 使用指定键名添加值:
$fruits = ['a' => 'apple', 'b' => 'banana'];
$fruits['c'] = 'cherry';

无论使用哪种方法,添加值后,数组将会包含新的值。



发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。