我有以下代码:
$tab=array(
"january"=>array(300,420,530,400),
"february"=>array(420,440,410,380),
"march"=>array(330,310,250)
);
如何将另一个元素添加到名为“april”的$tab
数组中,值为280和290?
答案 0 :(得分:2)
试试这个
$tab=array(
"january"=>array(300,420,530,400),
"february"=>array(420,440,410,380),
"march"=>array(330,310,250)
);
$tab["april"] = array(280,290);
答案 1 :(得分:0)
在PHP中,只需执行赋值即可添加到关联数组中。例如:
$tab['april'] = array(280, 290);
请注意,较新的PHP版本支持简洁的[]简写符号而不是数组函数,因此您可以编写:
$tab = ["january"=>[300, 420, 530, 400],
"february"=>[420, 440, 410, 380],
"march"=>[330, 310, 250]
];
$tab["april"] = [280, 290];