'Why is this PHP array not the same?

I'm not understanding why the array:

<? $data = array( 'items[0][type]' => 'screenprint'); ?>

Is not the same as

<? echo $data['items'][0]['type']; ?>

I'm trying to add to the array but can't seem to figure out how?



Solution 1:[1]

array( 'items[0][type]' => 'screenprint')

This is an array which has one key which is named "items[0][type]" which has one value. That's not the same as an array which has a key items which has a key 0 which has a key type. PHP doesn't care that the key kinda looks like PHP syntax, it's just one string. What you want is:

$data = array('items' => array(0 => array('type' => 'screenprint')));

I hope it's obvious that that's a very different data structure.

Solution 2:[2]

It should be:

$data = [
    'items' => [['type' => 'screenprint']]
];
echo $data['items'][0]['type'];

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 deceze
Solution 2 halfer