|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2003-01-28 04:58 UTC] messju at lammfellpuschen dot de
<?php
$a = array(-1=>1,
'-1'=>2,
0=>3,
'0'=>4,
1=>5,
'1'=>6
);
print_r($a);
?>
produces:
Array
(
[-1] => 1
[-1] => 2
[0] => 4
[1] => 6
)
foreach (array_keys($a) as $key) echo $key, ': ', gettype($key), "\n";
produces:
-1: integer
-1: string
0: integer
1: integer
so it seems strings containing negative integers do not get converted to
integers automagically but positive integers are.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Oct 29 23:00:01 2025 UTC |
<?php $a = array( '-1' => '-1', '0' => '0', '1' => '1', ); var_dump($a); ?> Output: array(3) { ["-1"]=> string(2) "-1" [0]=> string(1) "0" [1]=> string(1) "1" } (the '0' and '1' indexes are converted to integers..) Not sure if this is actually expected behaviour. :)I have an array which I use for long Binary Tree. I am pasting the array here which shows the negative key. The code was very general $i=0; while ($MyCondition) { $i=$i*2+1; temparray[$i]= $MyItem; $i=$i*2+2; temparray[$i]= $MyItem; } and the output has one negative key in it.. " [-2147483648] => "I have an array which I use for long Binary Tree. I am pasting the array here which shows the negative key. The code was very general $i=0; while ($MyCondition) { $i=$i*2+1; temparray[$i]= $MyItem; $i=$i*2+2; temparray[$i]= $MyItem; } and the output has one negative key in it.. " [-2147483648] => " Thanks and Regards Sushant Danekar http://Splendornet.com http://vidility.com/blog