Bug #64941 [Opn->Nab]: in_array fails to find index
| From: | nikic@php.net | Date: | Wed, 29 May 2013 08:43:46 +0000 |
| Subject: | Bug #64941 [Opn->Nab]: in_array fails to find index | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-179202@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=64941&edit=1
ID: 64941
Updated by: nikic@php.net
Reported by: mclaughlinm at byui dot edu
Summary: in_array fails to find index
-Status: Open
+Status: Not a bug
Type: Bug
Package: Arrays related
Operating System: Windows
PHP Version: 5.4.15
Block user comment: N
Private report: N
New Comment:
Sorry, but your problem does not imply a bug in PHP itself. For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system
is not the
appropriate forum for asking support questions. Due to the volume
of reports we can not explain in detail here why your report is not
a bug. The support channels will be able to provide an explanation
for you.
Thank you for your interest in PHP.
in_array looks for array values, not keys. You are looking for either isset() or array_key_exists().
Previous Comments:
------------------------------------------------------------------------
[2013-05-29 06:44:27] mclaughlinm at byui dot edu
Description:
------------
---
From manual page: http://www.php.net/function.in-array#refsect1-function.in-array-returnvalues
---
Tested in 5.3 and 5.4 with the results that a numeric and string index aren't found in an
array. Returns false when it should be true. There is no "Undefined index" error raised.
Test script:
---------------
testcase1.php
=============
<?php
$musketeers =
array("Noble"=>"Athos","Priest"=>"Aramis"
,"Dandy"=>"Porthos","Hero"=>"d'Artagnan");
$search = "Dandy";
if (in_array($search,$musketeers))
print "[Is Found] [".$musketeers[$search]."]\n";
else
print "[Not Found][".$musketeers[$search]."]\n";
?>
testcase2.php
=============
<?php
$musketeers =
array("Athos","Aramis","Porthos","d'Artagnan");
$search = "2";
if (in_array($search,$musketeers))
print "[Is Found] [".$musketeers[$search]."]\n";
else
print "[Not Found][".$musketeers[$search]."]\n";
?>
Expected result:
----------------
It should return true when finding the index value in the array.
Actual result:
--------------
Doesn't return true when finding the index value in the array, but doesn't raise an
Undefined index error.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=64941&edit=1