5

Let's say I define an array:

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

The output is fairly expected. I assume that there is some sort of strval() or intval() going on internally to allow the value to be overwritten.

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

But this is what confuses me.

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

This gives me:

integer
string

How is it that PHP internally makes those type conversions when using Array keys? i.e., How does it determine the key is a "valid decimal integer" as per the documentation? Also, is_int doesn't work on strings.

6
  • 6
    It's PHP, what did you expect? Commented Jan 30, 2013 at 18:56
  • My thoughts exactly. It doesn't make much sense, really. Commented Jan 30, 2013 at 18:57
  • There is method to the madness. I would like to know the method. Pun intended. Commented Jan 30, 2013 at 18:57
  • 1
    PHP looks at the contents of a variable first, rather than type. That's why 0 == '0' but 0 !== '0'. In the case of arrays $a['key'] can be written as $a[key] you'll just get an undefined constant warning, which unfortunately can be suppressed. Commented Jan 30, 2013 at 19:01
  • 1
    This is a great example of why you should not mix integer and string array indexes. Commented Jan 30, 2013 at 19:05

1 Answer 1

9

All explained in the php manual

An array in PHP is actually an ordered map.

The key can either be an integer or a string.

A few typecasts are performed (even on strings)

Strings containing valid integers will be cast to the integer type. E.g. the key "8" will actually be stored under 8. On the other hand "08" will not be cast, as it isn't a valid decimal integer.

Floats are also cast to integers, which means that the fractional part will be truncated. E.g. the key 8.7 will actually be stored under 8.

Bools are cast to integers, too, i.e. the key true will actually be stored under 1 and the key false under 0.

Null will be cast to the empty string, i.e. the key null will actually be stored under "".

Arrays and objects can not be used as keys. Doing so will result in a warning: Illegal offset type.

EDIT

If you want to know more about how PHP handles arrays internally, I recommend this article

Sign up to request clarification or add additional context in comments.

5 Comments

What a terrible way to do business.
Don't forget php is a scripting language which started as a templating engine, powerful in string manipulation. It's getting much better already :))
I read that part. I understand the that it is cast... just not how. What is it that determines a "valid decimal integer"?
@phil everything that matches regexp [1-9][0-9]* (within range) is considered a valid decimal integer, don't know how this is cast internally tho
@MichelFeldheim yeah... that's what I'm looking for. I would like to use it myself haha.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.