From: ionut dot dumitru at webland dot ro
Operating system: vista with latest wamp
PHP version: 5.2.6
PHP Bug Type: Arrays related
Bug description: some king of internal type casting issue i guess
Description:
------------
when dealing with a complex array that had nested levels with either 0
based index arrays or string based keys, i had to recursively parse each
nested level to transform it in another layout. the parsing method used
foreach regardless if it was a 0 based index or a string based one and at
some levels that had a key 'id' i wanted to get rid of that. so i was
testing if($key!='id') but that brakes the foreach in case of a 0 based
indexed array. the solution was to add if(is_int($key) || $key!='id').
my impression is that something is happening when $key is an int in the
case of 0 based index array and is compared to a string.
i have attached in the code box a smaller code that seems to show the
problem by not echoing the first element of the array. if needed i will try
to recreate the more complex code with the recursive parser.
Reproduce code:
---------------
<?php
$ar=array('a','b','c','d');
foreach($ar as $key => $value)
{
echo 'ar['.$key.']='.$value.'<br>';
}
echo '<br><br>';
//should work but it doesn't
foreach($ar as $key => $value)
{
if($key!='whatever string')
{
echo 'ar['.$key.']='.$value.'<br>';
}
}
echo '<br><br>';
//this works
foreach($ar as $key => $value)
{
if(is_int($key) || $key!='whatever string')
{
echo 'ar['.$key.']='.$value.'<br>';
}
}
?>
Expected result:
----------------
ar[0]=a
ar[1]=b
ar[2]=c
ar[3]=d
ar[0]=a
ar[1]=b
ar[2]=c
ar[3]=d
ar[0]=a
ar[1]=b
ar[2]=c
ar[3]=d
Actual result:
--------------
ar[0]=a
ar[1]=b
ar[2]=c
ar[3]=d
ar[1]=b
ar[2]=c
ar[3]=d
ar[0]=a
ar[1]=b
ar[2]=c
ar[3]=d
--
Edit bug report at http://bugs.php.net/?id=45475&edit=1
--
Try a CVS snapshot (PHP 5.2):
http://bugs.php.net/fix.php?id=45475&r=trysnapshot52
Try a CVS snapshot (PHP 5.3):
http://bugs.php.net/fix.php?id=45475&r=trysnapshot53
Try a CVS snapshot (PHP 6.0):
http://bugs.php.net/fix.php?id=45475&r=trysnapshot60
Fixed in CVS: http://bugs.php.net/fix.php?id=45475&r=fixedcvs
Fixed in release:
http://bugs.php.net/fix.php?id=45475&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=45475&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=45475&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=45475&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=45475&r=support
Expected behavior: http://bugs.php.net/fix.php?id=45475&r=notwrong
Not enough info:
http://bugs.php.net/fix.php?id=45475&r=notenoughinfo
Submitted twice:
http://bugs.php.net/fix.php?id=45475&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=45475&r=globals
PHP 4 support discontinued: http://bugs.php.net/fix.php?id=45475&r=php4
Daylight Savings: http://bugs.php.net/fix.php?id=45475&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=45475&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=45475&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=45475&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=45475&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=45475&r=mysqlcfg