#31927 [Opn->Bgs]: in_array() not working with array containing strings as needle
| From: | tony2001@php.net | Date: | Sat, 12 Feb 2005 13:45:50 +0000 |
| Subject: | #31927 [Opn->Bgs]: in_array() not working with array containing strings as needle | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-73937@lists.php.net to get a copy of this message | ||
ID: 31927
Updated by: tony2001@php.net
Reported By: bart at mediawave dot nl
-Status: Open
+Status: Bogus
Bug Type: Arrays related
Operating System: WinXP
PHP Version: 5CVS-2005-02-11 (dev)
New Comment:
>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.
Previous Comments:
------------------------------------------------------------------------
[2005-02-12 01:16:38] 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-11 20:52:14] 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-11 05:53:04] 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
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=31927&edit=1