Update. My initial intention on this issue was to determine if PHP really has this function. This was lost in the focus of answers to the scalar problem. Instead, you can see this new question: "Does PHP have autovivization?" This question is left here for reference.
According to Wikipedia , PHP does not have auto-rendering, but this code works:
$test['a']['b'] = 1; $test['a']['c'] = 1; $test['b']['b'] = 1; $test['b']['c'] = 1; var_dump($test);
Output:
array 'a' => array 'b' => int 1 'c' => int 1 'b' => array 'b' => int 1 'c' => int 1
I found that this code also works:
$test['a'][4] = 1; $test['b'][4]['f'] = 3;
But adding this line raises a warning ("Warning: you cannot use a scalar value as an array")
$test['a'][4]['f'] = 3;
What's going on here? Why does this happen when I add an associative element after the index? Is this a โtrueโ Perl-like auto-vivification or a variation of it or something else?
Edit: oh, now I see an error with a scalar, oops! They work as expected:
$test['a'][4]['a'] = 1; $test['a'][4]['b'] = 2; $test['a'][5]['c'] = 3; $test['a'][8]['d'] = 4;
So php has autovivitation? A Google search for โphp auto-imagingโ does not trigger a canonical answer or its example.
php autovivification
Ollie glass
source share