The problem with the PHP array where the keys are large integers

What I'm doing here basically starts with $statsand gets an array $countscontaining four arrays, where each array is a pair: is a timestamp in milliseconds , while values ​​are values. I am using PHP version 5.3.14 for Windows 7 x64.

Question: why do I get key values ​​of keys of negative values ​​and how can this be avoided? See below var_dump():

$stats = array();
$stats[] = array(
    'subtype' => 'small_text_message_user',
    'count'   => '6',
    'date'    => '2012-06-03'
);

$stats[] = array(
    'subtype' => 'small_text_message_auto',
    'count'   => '3',
    'date'    => '2012-07-03',
);

$stats = array(
    'subtype' => 'newsletter_auto',
    'count' => '11',
    'date' => '2012-07-16',
);

$counts = array();
$counts['small_text_message_user'] = array();
$counts['small_text_message_auto'] = array();
$counts['newsletter_user']         = array();
$counts['newsletter_auto']         = array();

foreach($data as $stat) :
    $millisecs = 1000 * strtotime($stat['date']);
    $count     = intval($stat['count']);
    $counts[$stat['subtype']][$millisecs] = $count;
    var_dump($millisecs, $count);
endforeach;

var_dump($counts);

And the result is incorrect:

float 1338674400000

int 6

float 1341266400000

int 3

float 1342389600000

int 11

array (size=4)
  'small_text_message_user' => 
    array (size=1)
      -1355396352 => int 6
  'small_text_message_auto' => 
    array (size=1)
      1236603648 => int 3
  'newsletter_user' => 
    array (size=0)
      empty
  'newsletter_auto' => 
    array (size=1)
      -1935163648 => int 11

An example (which works, actually) is a short piece of code in which I create an array of "dummy":

public function createTimestampRangeFromDates(\DateTime $from,
    \DateTime $to = null)
{
    $start = strtotime($from->format('Y-m-d 00:00:00'));
    $limit = strtotime(sprintf('%s 00:00:00', $to ? $to->format('Y-m-d')
        : date('Y-m-d') . ' 00:00:00'));

    return range($start, $limit, 86400);
}

// Test
$start = new \DateTime('2012-06-27');
$end   = new \DateTime('2012-07-07'); // 10 days

$timestamps = createTimestampRangeFromDates($start, $end);
$millisecs  = array_combine($timestamps, array_fill(0, count($timestamps), 0));

var_dump($millisecs);

This one works fine and I get large integers as keys:

array (size=11)
  '1340748000000' => int 0
  '1340834400000' => int 0
  '1340920800000' => int 0
  '1341007200000' => int 0
  '1341093600000' => int 0
  '1341180000000' => int 0
  '1341266400000' => int 0
  '1341352800000' => int 0
  '1341439200000' => int 0
  '1341525600000' => int 0
  '1341612000000' => int 0

, , - - :

$values = array_merge($millisecs, $counts['newsletter_auto']);

.

, @hakre

$result1 = array();
$result2 = array();
$test    = array('2012-06-03', '2012-07-03', '2012-07-16');

foreach($test as $date) :
    $result1[1000 * strtotime($date)] = 0;
    $result2[] = 1000 * strtotime($date);
endforeach;

var_dump($result1); // Not working
var_dump($result2); // Works

$result1. , ( float):

array (size=3)
  -1355396352 => int 0
  1236603648 => int 0
  -1935163648 => int 0

array (size=3)
  0 => float 1338674400000
  1 => float 1341266400000
  2 => float 1342389600000
+5
4

PHP nummeric (so, float integer) integer:

ckruse@lin ~ $ php -r 'var_dump(array(1.2 => "a"));'
array(1) {
  [1]=>
  string(1) "a"
}

, , float PHP , . , .

:

ckruse@lin ~ $ php -r 'var_dump(array(((string)1.2) => "a"));'
array(1) {
  ["1.2"]=>
  string(1) "a"
}

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

, , . . 8.7 8.

+7

- PHP, ?

PHP : " ". ()

, . . , , , , .

. , .

PHP_INI_Min PHP_INI_MAX.

, , 2 . .

+1

, , , . , , , . .

() , float integer, .

EDIT: PHP:

. .

, :

, , . . 8.7 8.

0

All Articles