0

我有一个数组:

$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
                )
         (

如何才能做到这一点?感谢并为我糟糕的英语感到抱歉。

4

1 回答 1

1

您提供的代码无法解析。

确保$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);

输出:

Array
(
    [foo] => Array
        (
            [totalsales] => 80
            [totalamount] => 4
            [2013-06-30] => 20
            [2013-06-25] => 40
        )
)
于 2013-06-30T17:12:44.473 回答