php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #30036 In_Array not works when array contain booleans
Submitted: 2004-09-09 10:04 UTC Modified: 2004-09-15 11:36 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: marek at lewczuk dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 4.3.8 OS: Windows
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: marek at lewczuk dot com
New email:
PHP Version: OS:

 

 [2004-09-09 10:04 UTC] marek at lewczuk dot com
Description:
------------
When you have an array with boolean values, then in_array will always return true.

Reproduce code:
---------------
print in_array("test it", array(true, true, "sdsd" => true))

Expected result:
----------------
should return false

Actual result:
--------------
true

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-09-15 10:56 UTC] sniper@php.net
Use the 3rd optional parameter (for strict search) which makes  
in_array() to check the type too. NOT a bug.

 [2004-09-15 11:36 UTC] marek at lewczuk dot com
Right, it works with 3rd parameter, but it is not the point - is string "test" equal to boolean "true" ? Even if we are not comparing variable types, then "test" is not equal to "1". For example, I have an array with strings, integers, boolean - I'm looking for any "123" value - I don't care if this is an integer or string - the value is important. In this case, I can't use in_array with 3rd parameter, because it will not find all all correct values. 

examples:

in_array('232', array(232, '232', true))
this will return true - correct

in_array('232', array(232, true), true)
this will return false - correct

in_array('232', array(233, '233', true))
this will return true - wrong

In my opinion this is not correct behavior...
 [2011-01-15 00:53 UTC] miker at yahoo-inc dot com
sniper@php.net, can you describe the reasoning behind this behavior? Currently, I'm inclined to agree with Marek that this is bug-like behavior. Thank you. Mike.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun May 05 14:01:31 2024 UTC