PHP将特定键值添加到预先存在的数组中
我有一个数组:PHP将特定键值添加到预先存在的数组中,php,arrays,insert,Php,Arrays,Insert,我有一个数组: $test = Array ( ["foo"] => Array ( ["totalsales"] => 80 ["totalamount"] => 4 ) ( 我想添加具有以下值的新索引: $test["foo"][$date] = 20; // $date
$test = Array
(
["foo"] => Array
(
["totalsales"] => 80
["totalamount"] => 4
)
(
我想添加具有以下值的新索引:
$test["foo"][$date] = 20; // $date = 2013-06-30
$test["foo"][$date] = 40; // $date = 2013-06-25
输出如下所示:
$test = Array
(
["foo"] => Array
(
["totalsales"] => 80
["totalamount"] => 4
["2013-06-25"] => 40
)
(
$test = Array
(
["foo"] => Array
(
["totalsales"] => 80
["totalamount"] => 4
["2013-06-30"] => 20
["2013-06-25"] => 40
)
(
我希望阵列的外观如下所示:
$test = Array
(
["foo"] => Array
(
["totalsales"] => 80
["totalamount"] => 4
["2013-06-25"] => 40
)
(
$test = Array
(
["foo"] => Array
(
["totalsales"] => 80
["totalamount"] => 4
["2013-06-30"] => 20
["2013-06-25"] => 40
)
(
如何做到这一点?谢谢你,我的英语很差 您提供的代码无法解析 确保
$date
变量准确地包含它应该包含的内容,因为(除了语法问题)您的示例工作得非常好:
<?php
$test = array
(
'foo' => array
(
'totalsales' => 80,
'totalamount' => 4
)
);
$date = '2013-06-30';
$test['foo'][$date] = 20;
$date = '2013-06-25';
$test['foo'][$date] = 40;
print_r($test);
除非您有语法错误,否则它应该可以正常工作。你做得不对。或者你没有更新日期?谢谢你的快速回复。我的代码的前一部分中有一个错误:-)