php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #31927 in_array() not working with array containing strings as needle
Submitted: 2005-02-11 05:53 UTC Modified: 2005-02-13 21:26 UTC
From: bart at mediawave dot nl Assigned:
Status: Not a bug Package: Documentation problem
PHP Version: 5CVS-2005-02-11 (dev) OS: WinXP
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: bart at mediawave dot nl
New email:
PHP Version: OS:

 

 [2005-02-11 05:53 UTC] bart at mediawave dot nl
Description:
------------
in_array() doesn't work with an array containing strings (larger then one character) as a needle.

Reproduce code:
---------------
<?php

$a = array(array("Mac", "NT"), array("Irix", "Linux"), "OS2");

if (in_array(array("NT", "Linux"), $a)) {
   echo "NT Linux found\n";
}

if (in_array(array("XP", "Unix"), $a)) {
   echo "XP Unix found\n";
}

if (in_array("OS2", $a)) {
   echo "OS2 found\n";
}

?> 

Expected result:
----------------
NT Linux found
OS2 found

Actual result:
--------------
OS2 found

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-02-11 20:52 UTC] tony2001@php.net
No bug here: in_array() looks for exact match.

This code works fine:
<?php
$a = array(array("NT", "Linux"), array("Irix", "Linux"), "OS2");

if (in_array(array("NT", "Linux"), $a)) {
   echo "NT Linux found\n";
}

?>
 [2005-02-12 01:16 UTC] bart at mediawave dot nl
The documentation clearly states:

in_array -- Checks if a "value" exists in an array

It doesn't say:

in_array -- Checks if a "variable" exists in an array

An array itself isn't a value. It's a variable containing a collection of values. So, one would expect that in_array checks if all these "values" exist in the haystack. (Regardless of the containing variable type/structure) It shouldn't check if a variable exists in the haystack. 

The exact match behaviour in your example should only happen when the parameter strict is set. (In my opinion at least)
 [2005-02-12 14:45 UTC] tony2001@php.net
>An array itself isn't a value. 
Nope. Don't know who told you this, but he/she was definitely wrong.

>So, one would expect that in_array checks if all these
>"values" exist in the haystack.
No, see examples in the docs. 

No bug here.
 [2005-02-12 22:22 UTC] bart at mediawave dot nl
You are right. It isn't a bug. in_array was apparently designed to work this way and does so properly.

Maybe we should change this to a change/feature request then? Or otherwise a documentation problem? It simply isn't intuitive for people now.

This is my last attempt. I won't be re-opening this bug anymore.
 [2005-02-12 22:32 UTC] tony2001@php.net
Well, probably this should be clarified in the docs.
But the current description looks rather clear to me, though.
Reclassifying as docu problem.
 [2005-02-13 21:26 UTC] vrana@php.net
No bug here.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Jul 09 14:01:33 2025 UTC