php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #64049 problem with in_array when mixing string and integer results
Submitted: 2013-01-22 18:23 UTC Modified: 2013-01-25 15:28 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: mrmateopers at gmail dot com Assigned:
Status: Not a bug Package: hash related
PHP Version: 5.5.0alpha2 OS: Debian, Ubuntu, Windows 7
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: mrmateopers at gmail dot com
New email:
PHP Version: OS:

 

 [2013-01-22 18:23 UTC] 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


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-01-23 03:35 UTC] 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-23 03:35 UTC] laruence@php.net
-Status: Open +Status: Not a bug
 [2013-01-25 15:28 UTC] mrmateopers at gmail dot com
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
 [2013-01-25 15:28 UTC] mrmateopers at gmail dot com
-: carles dot mateo at gmail dot com +: mrmateopers at gmail dot com
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Dec 27 03:01:28 2024 UTC