1

I'm facing a array structure problem here.

Why this array

$plans = array(
    'id'   => 'free', 
    'name' => 'Free', 
    'sums' => array(
        'usd' => 0,
    ),
    'id'   => 'trial', 
    'name' => 'Trial', 
    'sums' => array(
        'usd' => 0,
    ),
);

returns me only this (last result of my array):

Array
(
    [id] => trial
    [name] => Trial
    [sums] => Array
        (
            [usd] => 0
        )

)

Any help with this will be very appreciated.

Thanks a lot.

2
  • 2
    You can't use the same key more than once in an associative array. You need to use an array of arrays. Commented Jan 12, 2018 at 19:21
  • same keys overwrites with latest value Commented Jan 12, 2018 at 19:22

1 Answer 1

7

That is because you overwrite the array keys in each array item effectively removing the value before it. You need an array of arrays for this data:

$plans = array(
    array(
        'id'   => 'free', 
        'name' => 'Free', 
        'sums' => array(
            'usd' => 0,
        )
    ),
    array(
        'id'   => 'trial', 
        'name' => 'Trial', 
        'sums' => array(
            'usd' => 0,
        )
    )
);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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