A
A
Alexey Berdnikov2012-08-10 09:49:46
PHP
Alexey Berdnikov, 2012-08-10 09:49:46

var_export array bug when its keys are negative numbers?

<?php
$data = array('moder'=> array(
    -2 => array(
        2=>1,
        1=>1,
        0=>1,
        -1=>-1,
        -2=>-2,
    )
));
print '<pre>';
print '<h1>print_r</h1>';
print_r($data);

print '<h1>var_dump</h1>';
var_dump($data);
print '</pre>';

print '<h1>var_export (BAD keys!!!)</h1>';
$code = '<?php
return '.var_export($data, true);
print highlight_string($code);

print '<h1>serialize</h1>';
print serialize($data);

print '<h1>some script</h1>';
highlight_file(__FILE__);

Result (only different parts here):
PHP 5.3.2-1ubuntu4.11 with Suhosin-Patch (cli) (built: Dec 13 2011 18:49:27)
#24-Ubuntu SMP Fri Jan 7 18:30:50 UTC 2011 x86_64 GNU/Linux
<?php
return array (
  'moder' => 
  array (
    18446744073709551614 => 
    array (
      2 => 1,
      1 => 1,
      0 => 1,
      18446744073709551615 => -1,
      18446744073709551614 => -2,
    ),
  ),
)

PHP 5.3.10-1ubuntu3.2 with Suhosin-Patch (cli) (built: Jun 13 2012 17:20:55)
#43-Ubuntu SMP Fri Jul 6 15:06:05 UTC 2012 i686 i686 i386 GNU/Linux
<?php
return array (
  'moder' => 
  array (
    -2 => 
    array (
      2 => 1,
      1 => 1,
      0 => 1,
      -1 => -1,
      -2 => -2,
    ),
  ),
)

How to achieve the same expected results regardless of the platform?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Melkij, 2012-08-10
@Groove

The bug was in 5.2.x, closed in 5.3.4. bugs.php.net/bug.php?id=52534

N
Nazar Mokrinsky, 2012-08-10
@nazarpc

Apparently, the bug was in 5.3.2, and in 5.3.10 it has already been fixed.
System x86 or x64?

N
Nazar Mokrinsky, 2012-08-10
@nazarpc

Try to specify the keys not as -1 , but as '-1' - as strings.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question