PHP auto-visualization

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.

+8
php autovivification
source share
3 answers

In the PHP manual for square bracket syntax:

$arr[] = value;

If $arr does not exist yet, it will be created, so this is also an alternative way to create an array

In your example:

 $test['a'][4] = 1; 

Since $test and $test['a'] do not currently exist; they are created as arrays.

 $test['b'][4]['f'] = 3; 

$test['b'] and $test['b'][4] do not currently exist; they are created as arrays.

 $test['a'][4]['f'] = 3; 

$test['a'][4] exists, but it is an integer ( 1 ). This is a "scalar value" that cannot be used as an array. You cannot use the square bracket [] syntax for numbers; it does not convert the existing value to an array.

+6
source share

According to the results, PHP has autovivitation. The error is due to how it works.

When you say: $ a [1] [2] = 55, PHP wants to insert 55 into $ a [1] as [2] => 55. Since $ a [1] does not exist, PHP automatically creates an empty node, cca. $ a [1] = Array (). But when node already exists, PHP does not create $ a [1], it just executes [2] => 55, which is an error if $ a [1] is not an array (array, object).

The last language I saw where nodes can be valuable and children too is MUMPS. There was also a function called $ DATA () that indicated that node has any child element (10), value (1) or both (11), or it does not exist (0). I think the correct handling of associative arrays.

(Anyway, I like this PHP behavior.)

+2
source share

FROM

 $test['b'][4]['f'] = 3; 

You are not adding an item to

 $test['a'][4] 

bacause is not initialized as an array.

If you write:

 $test['a'][4] = array(1); 

Then it will work.

Through:

 $test['a']['b'] = 1; $test['a']['c'] = 1; $test['b']['b'] = 1; $test['b']['c'] = 1; 

You implicitly initialize $test['a'] and $test['b'] as an array. But $test['a']['b'] (etc.) as an int

+1
source share

All Articles