How does PHP internally use keys and array types?

Let's say I define an array:

$a = Array();
$a[0] = 1;
$a['0'] = 2;
$a['0a'] = 3;
print_r($a);

Conclusion of the expected result. I suppose there is something like strval()or inside intval()to allow overwriting the value.

Array
(
    [0] => 2
    [0a] => 3
)

But this is what bothers me.

foreach($a as $k => $v) {
    print(gettype($k) . "\n");
}

This gives me:

integer
string

How is it that PHP does these type conversions when using Array keys? that is, how to determine the key is a "real decimal integer" according to the documentation? Also is_intdoes not work with strings.

+5
source share
1 answer

Everything is explained in php manual

An array in PHP is actually an ordered map.

The key can be an integer or a string.

Several types are executed (even line by line)

, , . . "8" 8. , "08" , .

, , . . 8.7 8.

Bools , .. true 1 false 0.

Null , .. "".

. : .

, PHP ,

+9
source

All Articles