Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am having a problem with an array changing the value of keys on me. In the example I'm providing below it starts out ok, but at some point php is turning my string into an integer and changing that integer when inserting it into another array as a key.

Code
-----------------
$fund = '01';
$division = '1';
$gl_transactions[$fund] = array();
$glint = array($division=>array('gl_cash_account'=>'1800001040000000')); 
print chr(10).'1:'.chr(10); 
print_r($glint); 
$gl = $glint[$division]['gl_cash_account']; 
print chr(10).'2:'.chr(10); 
print $gl;
$gl_transactions[$fund][$gl]['description'] = 'MTS DISBURSEMENTS'; 
print chr(10).'3:'.chr(10);
print_r($gl_transactions);

Expected output 
--------------- 
1:Array([1] => Array([gl_cash_account] => 1800001040000000 ))
2:1800001040000000  
3:Array([01] => Array([1800001040000000] => Array([description] => MTS DISBURSEMENTS))) 

Actual output 
--------------- 
1:Array([1] => Array([gl_cash_account] => 1800001040000000 ))
2:1800001040000000  
3:Array([01] => Array([1721082880] => Array([description] => MTS DISBURSEMENTS))) 

notice the index changed from 1800001040000000 to 1721082880

Obsticles

These big numbers are G/L Account numbers and cannot be changed to smaller numbers. We have tens of thousands of pieces of code, over a million lines of code in our product and cannot go through every bit of it to find where this may be an issue and rewrite it. This is just a generic example of something that we do in many places, building multidimensional arrays using data from a database. I can simply cast the variable as a string when inserting into the array to fix the above example, but backfilling 1M+ lines of code is not a viable option.

On my development machine I run php 5.3 MSSQL, Windows, and IIS. I do not get this error with the above code but I do get it when simply setting a variable as an integer and then inserting it into an array as a key. i.e. $gl = 1800001040000000; $ar[$gl] = 1; print_r($ar); Now we don't cast a variables to integers in our software, but in the first example php converts it on it's own at some point when it's building the last array when ran on some of our clients systems.

So my questions are:

  1. What is the exact limit of a numeric array key.
  2. Is there a way to increase this.
  3. Has this been fixed or increased in later versions of php beyond 5.2

Other notes

Our software works with several databases, several browsers, window and linux, apache and IIS. We have hundreds of customers using the software all with their own unique setup. Most of our customers are on php 5.2 currently and upgrading them is not possible at this point because of deprecated functions used in our software.

share|improve this question
    
Assigning a numeric string as array key will trigger PHPs loose typing. Easy fix here: use a 64 bit PHP binary. Or prefix your numeric keys with another character. –  mario Jul 31 '13 at 0:47
    
possible duplicate of A numeric string as array key in PHP –  mario Jul 31 '13 at 0:50

1 Answer 1

My guess is that you are limited by the maximum value of an integer since PHP will automatically cast strings with valid integer values being used as array keys to integers. The PHP manual indicates the limits for integers will vary by system, but the big issue is 32-bit systems vs. 64-bit systems in determining upper limit.

http://php.net/manual/en/language.types.integer.php

The maximum integer value on a system can be read from PHP_INT_MAX constant.

share|improve this answer
    
@mario There are no "supported" 64 bit PHP binaries that I'm aware of and we would have to upgrade all of our customers (which they typically DO NOT want to do) so not an easy fix at all. Prepending the index would require reviewing/altering thousands of programs so not an easy fix either. –  Christian Mauro Aug 1 '13 at 19:51
    
@ChristianMauro Not sure there is a quick solution for you, really should have been a design consideration form the get go, as it is generally bad form to use strings with integer values as keys if you want them to behave like strings. –  Mike Brant Aug 1 '13 at 19:55
    
you are exactly correct in that the integer limit is the numeric array key limit. I have also done further tests on several different setups and founf the problem in Linux Centos5 32 bit setups. I've tried casting the vaiable as I set the array i.e. $ar[$fund][(string)$gl] = $data; and using double quotes i.e. $ar[$fund]["$gl"] = $data; Both do not fix the problem. Also not all number trigger the bug, the account 1800001040000000 does where 1800002180000100 does not. –  Christian Mauro Aug 1 '13 at 20:01

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.