ID:               45475
 Updated by:       [EMAIL PROTECTED]
 Reported By:      ionut dot dumitru at webland dot ro
-Status:           Open
+Status:           Bogus
 Bug Type:         Arrays related
 Operating System: vista with latest wamp
 PHP Version:      5.2.6
 New Comment:

Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

0 == "some string" => true, use === instead, see documentation.


Previous Comments:
------------------------------------------------------------------------

[2008-07-10 10:53:54] ionut dot dumitru at webland dot ro

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 this bug report at http://bugs.php.net/?id=45475&edit=1

Reply via email to