Join the Stack Overflow Community
Stack Overflow is a community of 6.5 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

By coincidence I ran into a very bizarre behaviour regarding PHP arrays and its keys. Consider this creation of an PHP array.

$arr[2250572483]=1;
//dump the array
var_dump($arr);
//Result:
array(1) { [-2044394813]=> int(1) }

Somehow the array key has changed its value to a completely different negative number. This led me to some further investigation which is still inconclusive.
In below example I loop between the number range 2250572300 and 2250572500. Time is scarces for me so I did not manage to pinpoint at what number this phenomenon starts occurring because I run out of memory looping through large range of numbers. I think it should be somewhere between 2100000000 and 4300000000.

$arr2 = array();
for($i=2250572300; $i<= 2250572500; $i++){
  $arr2[$i]=$i;
}
echo "<pre>".var_export($arr2,true)."</pre>";

My question is: does anyone know how and why this is happening and is there anything that is currently being done to fix the problem?

Essentially this is a major design flaw within PHP and could potentially make PHP useless when you are working with numbers in arrays, examples being supplier, invoice, item numbers etc.

Thanks

share|improve this question
    
Seems to work for me: 3v4l.org/6nD3C and the second example too: 3v4l.org/Feooi – WizKid May 22 '14 at 5:09

You're using an integer value as the array key. All integers in PHP are signed integers and on 32-bit systems, the maximum value is 232 - 1 (given by PHP_INT_MAX). If the integer value is greater than PHP_INT_MAX then it wraps over and gives $key % PHP_INT_MAX as the result.

To confirm:

echo 2250572483 % PHP_INT_MAX; // => -2044394813

The solution would be to use the key as a string, i.e. $arr['2250572483']=1;. This shouldn't be a problem on 64-bit systems, though (where the upper limit is 264 - 1).

share|improve this answer
    
Hi Amal, Simply putting quotes around a number does not make it a string in PHP hence why "3"*4=12 works. $arr['2250572483']=1 still has a problem. Doing things like $arr[(string)2250572483] does not work. I realize the problem is 32bit computer system but I should still be able to explicitly set 2250572483 to be a string value. PHP treats it as a number even it you have it inside quotes. You could do things like $arr["2250572483 "]=1 and then trim it, but why should I have to do workarounds like that. The problem is that PHP interprets a string as a number if all characters are numbers. – tuberider May 22 '14 at 23:57

That has something to do with how integers are saved. The first bit (from the left side) say if you are positiv + or negativ - and with this behavior the result with big ints will be how you discribted it. That you have a negativ Integer.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.