php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #45475 some king of internal type casting issue i guess
Submitted: 2008-07-10 10:53 UTC Modified: 2008-07-10 11:58 UTC
From: ionut dot dumitru at webland dot ro Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.2.6 OS: vista with latest wamp
Private report: No CVE-ID: None
 [2008-07-10 10:53 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-07-10 11:58 UTC] johannes@php.net
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.
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Thu May 28 08:01:25 2020 UTC