0

I have an array:

$test = Array
        (
            ["foo"] => Array
                (
                    ["totalsales"] => 80
                    ["totalamount"] => 4
                )
         (

I want to add new indices with values:

$test["foo"][$date] = 20; // $date = 2013-06-30
$test["foo"][$date] = 40; // $date = 2013-06-25

The output looks like this:

$test = Array
        (
            ["foo"] => Array
                (
                    ["totalsales"] => 80
                    ["totalamount"] => 4
                    ["2013-06-25"] => 40
                )
         (

I would expect the array to look like this:

$test = Array
        (
            ["foo"] => Array
                (
                    ["totalsales"] => 80
                    ["totalamount"] => 4
                    ["2013-06-30"] => 20
                    ["2013-06-25"] => 40
                )
         (

How can this be done? Thanks and sry for my bad english.

1
  • Unless you have syntax errors, it should work fine. You're not doing something correctly. Or maybe you don't update the date? Commented Jun 30, 2013 at 17:17

1 Answer 1

1

The code you provided doesn't parse.

Make sure $date variable contains exactly what it should, since (other than syntax problems) your example works perfectly fine:

<?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);

Outputs:

Array
(
    [foo] => Array
        (
            [totalsales] => 80
            [totalamount] => 4
            [2013-06-30] => 20
            [2013-06-25] => 40
        )
)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the quick reply. I had an error in a previous section of my code :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.