Bug #64049 [Nab]: problem with in_array when mixing string and integer results
| From: | mrmateopers at gmail dot com | Date: | Fri, 25 Jan 2013 15:28:26 +0000 |
| Subject: | Bug #64049 [Nab]: problem with in_array when mixing string and integer results | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-176244@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=64049&edit=1
ID: 64049
User updated by: mrmateopers at gmail dot com
Reported by: mrmateopers at gmail dot com
Summary: problem with in_array when mixing string and integer
results
Status: Not a bug
Type: Bug
Package: hash related
Operating System: Debian, Ubuntu, Windows 7
PHP Version: 5.5.0alpha2
Block user comment: N
Private report: N
New Comment:
You're right Laurence.
I was obfuscated.
It is because the loose comparison on string == 0 returns TRUE.
Even, I strongly suggest to update the documentation (in top of in_array) in order to remark it.
Thanks.
Best
Previous Comments:
------------------------------------------------------------------------
[2013-01-23 03:35:56] laruence@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
in_array does loose comparison, maybe you need to use the third argument of it.
http://us1.php.net/manual/en/function.in-array.php
------------------------------------------------------------------------
[2013-01-22 18:23:47] mrmateopers at gmail dot com
Description:
------------
It affects all versions from 5.3 to 5.5.0.a.2.
Problems is when using in_array in array that mixes integer and numeric values AND has alphanumeric
keys f.e. 'name' in loose mode. Results are crazy and unpredictable. Works fine when using
numeric index.
When using in_array with strict work fine, but not here:
// Create an array like:
$st_array = array( 'name1' => 0,
'name2' => 1,
'name3' => '2',
'name4' => 'Catalunya Lliure');
// Then look with in_array and be surprised
echo 'Case 0:'.in_array('0', $st_array)."\n";
echo 'Case 1:'.in_array('1', $st_array)."\n";
echo 'Case 2:'.in_array('2', $st_array)."\n";
echo 'Case n:'.in_array('n', $st_array)."\n";
echo 'Case z:'.in_array('z', $st_array)."\n";
echo 'Case Catalunya:'.in_array('Catalunya', $st_array)."\n";
echo 'Case num no:'.in_array(3, $st_array)."\n";
echo 'Case num yes:'.in_array(1, $st_array)."\n";
// More in deep in the next box
Test script:
---------------
<?php
$st_array = array( 'name1' => 0,
'name2' => 1,
'name3' => '2',
'name4' => 'Catalunya Lliure');
echo 'Case 0:'.in_array('0', $st_array)."\n";
echo 'Case 1:'.in_array('1', $st_array)."\n";
echo 'Case 2:'.in_array('2', $st_array)."\n";
echo 'Case n:'.in_array('n', $st_array)."\n";
echo 'Case z:'.in_array('z', $st_array)."\n";
echo 'Case Catalunya:'.in_array('Catalunya', $st_array)."\n";
echo 'Case num no:'.in_array(3, $st_array)."\n";
echo 'Case num yes:'.in_array(1, $st_array)."\n";
// Please note the different case of 'Catalunya' working fine with
// numeric auto index
$st_array2 = array(2,'a','Catalunya Lliure');
echo 'Case 1a:'.in_array(3, $st_array2)."\n";
echo 'case 2a:'.in_array(2, $st_array2)."\n";
echo 'case 3a:'.in_array('2', $st_array2)."\n";
echo 'Case 4a:'.in_array('Catalunya', $st_array2)."\n";
echo 'Case 5a:'.in_array('Catalunya Lliure', $st_array2)."\n";
Expected result:
----------------
Expected:
Case 0: 1
Case 1: 1
Case 2: 1
Case n:
Case z:
Case Catalunya:
Case num no:
Case num yes: 1
Case 1a:
Case 2a: 1
Case 3a: 1
Case 4a:
Case 5a: 1
Actual result:
--------------
Cas 0:1
Cas 1:1
Cas 2:1
Cas n:1
Cas z:1
Cas Catalunya:1
Cas num no:
Cas num sÃ:1
Cas 1a:
cas 2a:1
cas 3a:1
Cas 4a:
Cas 5a:1
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=64049&edit=1