| 
        php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login | 
 PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits             
             [2008-07-10 11:58 UTC] johannes@php.net
  | 
    |||||||||||||||||||||||||||
            
                 
                Copyright © 2001-2025 The PHP GroupAll rights reserved.  | 
        Last updated: Tue Nov 04 09:00:01 2025 UTC | 
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