Its very easy .... let me guess your Development system is windows and your production server is linux ?
You are having Integer overflow Issues because most likey your windows version of PHP is 32bit and linux is 64bit
See Condition for array key conversion
- 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.
So what happens is that :
So the key 28000000000000003
is a valid integer
on a 64bit
but a String
on a 32bits
system
I was able to replicate your issue
echo "<pre>";
$data = array("28000000000000003" => 'ABC',"28000000000000001" => 'PQR');
$keys = array("28000000000000003","28000000000000001");
$keysDerived = array_keys($data);
var_dump(in_array("28000000000000003", $keysDerived, true));
var_dump(in_array("28000000000000003", $keysDerived));
var_dump(in_array("28000000000000003", $keys, true));
var_dump(in_array("28000000000000003", $keys));
Output
bool(false) <----------------------- false instead of true
bool(true)
bool(true)
bool(true)
This issues has nothing to do with in_array
but rather array_keys
example
Sample Code
echo "<pre>";
$data = array("28000000000000003" => 'ABC',"28000000000000001" => 'PQR');
$keys = array("28000000000000003","28000000000000001");
$keysDerived = array_keys($data);
var_dump($keys,$keysDerived);
Output
array(2) {
[0]=>
string(17) "28000000000000003" <------- Keys are String
[1]=>
string(17) "28000000000000001"
}
array(2) {
[0]=>
int(28000000000000003) <------- They are converted to int on 64bits
[1]=>
int(28000000000000001)
}
See Online Demo
This means that they are not the same type ...
in_array bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )
If the third parameter strict is set to TRUE then the in_array() function will also check the types of the needle in the haystack.
If you run this code
foreach ( $keys as $key ) {
echo gettype($key) . "\n";
}
foreach ( $keysDerived as $key ) {
echo gettype($key) . "\n";
}
Output 64Bits
string
string
integer
integer
Output 32Bits
string
string
string
string
Simple Workaround
echo "<pre>";
$data = array("28000000000000003" => 'ABC',"28000000000000001" => 'PQR');
$keys = array("28000000000000003","28000000000000001");
$keysDerived = array_keys_string($data);
var_dump($keys,$keysDerived);
var_dump(in_array("28000000000000003", $keysDerived, true));
var_dump(in_array("28000000000000003", $keysDerived));
var_dump(in_array("28000000000000003", $keys, true));
var_dump(in_array("28000000000000003", $keys));
Output
array(2) {
[0]=>
string(17) "28000000000000003"
[1]=>
string(17) "28000000000000001"
}
array(2) {
[0]=>
string(17) "28000000000000003"
[1]=>
string(17) "28000000000000001"
}
bool(true)
bool(true)
bool(true)
bool(true)
See Original Code
See Modified Code
Function Used
function array_keys_string(array $input) {
$list = array();
foreach ( $input as $k => $v ) {
$list[] = (string)$k;
}
return $list;
}
array_map('var_dump', array_keys($data));
– One Trick Pony Feb 11 at 12:10