php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #64941 in_array fails to find index
Submitted: 2013-05-29 06:44 UTC Modified: 2013-05-29 08:43 UTC
From: mclaughlinm at byui dot edu Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.4.15 OS: Windows
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: mclaughlinm at byui dot edu
New email:
PHP Version: OS:

 

 [2013-05-29 06:44 UTC] 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.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-05-29 08:43 UTC] nikic@php.net
-Status: Open +Status: Not a bug
 [2013-05-29 08:43 UTC] nikic@php.net
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().
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri May 02 08:01:28 2025 UTC