Found strange things, for example create such array
$arSortFields=array('id','title);
and do such thing
echo intval(in_array(0,$arSortFields));
You will get "1" in echo! Thats means that element 0 is in array!
But if you do
echo intval(in_array(0,array());
you will got 0 as expected.
So be carefull to pass 0 as first parameter
in_array
(PHP 4, PHP 5)
in_array — Indique si une valeur appartient à un tableau
Description
Recherche needle dans haystack .
Liste de paramètres
- needle
-
La valeur recherchée.
Note: Si needle est une chaîne, la comparaison est faite en tenant compte de la casse.
- haystack
-
Le tableau.
- strict
-
Le troisième paramètre strict est optionnel. S'il vaut TRUE alors in_array() vérifiera aussi que le type du paramètre needle correspond au type de la valeur trouvée dans haystack .
Valeurs de retour
Retourne TRUE si needle est trouvé dans le tableau, FALSE sinon.
Historique
| Version | Description |
|---|---|
| 4.2.0 | needle peut maintenant être un tableau. |
Exemples
Exemple #1 Exemple avec in_array()
<?php
$os = array("Mac", "NT", "Irix", "Linux");
if (in_array("Irix", $os)) {
echo "Got Irix";
}
if (in_array("mac", $os)) {
echo "Got mac";
}
?>
La seconde condition échoue, car in_array() est sensible à la casse. Le script retourne :
Got Irix
Exemple #2 Exemple avec in_array() et le mode strict
<?php
$a = array('1.10', 12.4, 1.13);
if (in_array('12.4', $a, true)) {
echo "'12.4' est trouvé avec le mode strict\n";
}
if (in_array(1.13, $a, true)) {
echo "1.13 est trouvé avec le mode strict\n";
}
?>
L'exemple ci-dessus va afficher :
1.13 est trouvé avec le mode strict
Exemple #3 Exemple avec in_array() et un tableau en paramètre
<?php
$a = array(array('p', 'h'), array('p', 'r'), 'o');
if (in_array(array('p', 'h'), $a)) {
echo "'ph' a été trouvé\n";
}
if (in_array(array('f', 'i'), $a)) {
echo "'fi' was found\n";
}
if (in_array('o', $a)) {
echo "'o' a été trouvé\n";
}
?>
L'exemple ci-dessus va afficher :
'ph' a été trouvé 'o' a été trouvé
Voir aussi
- array_search() - Recherche dans un tableau la clé associée à une valeur
- isset() - Détermine si une variable est définie et est différente de NULL
- array_key_exists() - Vérifie si une clé existe dans un tableau
in_array
28-Nov-2009 02:17
28-Nov-2009 02:17
Found strange things, for example create such array
$arSortFields=array('id','title);
and do such thing
echo intval(in_array(0,$arSortFields));
You will get "1" in echo! Thats means that element 0 is in array!
But if you do
echo intval(in_array(0,array());
you will got 0 as expected.
So be carefull to pass 0 as first parameter
14-Nov-2009 11:14
This function has Probmen with UTF-8. To solve this Problem use:
in_array( mb_strtolower($tag, "UTF-8"), $arr)
16-Oct-2009 04:04
Needed to insert values at a specific point in another array, so I built this, it's untested on multidimensional arrays, but it works for my purposes:
<?php
/** inserts an array at a specific point in the other array. If you omit $after, it acts just like array merge.
* @param array $array array to merge into
* @param mixed $insert array or value to insert (non-array value is merged as array($value)
* @param string $after key to insert after. $key must be a valid key in $array or will default to merging at end like array_merge()
* @return array returns new array after merge/insert
*/
function array_insert($array, $insert, $after = '')
{
if (!array_key_exists ($after, $array))
{
return array_merge($array, $insert);
}
$new = array();
$lastkey = '';
foreach ($array as $key=>$value)
{
if ($after == $lastkey) {
$new = array_merge($new, (array) $insert);
}
$new[$key] = $value;
$lastkey = $key;
}
return $new;
}
?>
15-Oct-2009 10:21
I know this will probably be very slow over large datasets but I think I have a fairly simple way of checking whether or not a string is in a simple array.
It's both case-insensitive and works like a "LIKE" function:
<?php
$bool = stristr(implode(' ', $array), $word);
?>
The "space" as glue is unnecessary but it's doesn't matter either way.
05-Oct-2009 07:53
If you're creating an array yourself and then using in_array to search it, consider setting the keys of the array and using isset instead since it's much faster.
<?php
$slow = array('apple', 'banana', 'orange');
if (in_array('banana', $slow))
print('Found it!');
$fast = array('apple' => 'apple', 'banana' => 'banana', 'orange' => 'orange');
if (isset($fast['banana']))
print('Found it!');
?>
21-Sep-2009 05:23
Here's a function to see if all needles of an array are in a haystack:
<?php
function array_in_array($needles, $haystack) {
$matched_needles = 0;
foreach ($needles as $needle) {
if ( in_array($needle, $haystack) ) $matched_needles++;
}
return ($matched_needles == count($needles)) ? true : false;
}
?>
14-Sep-2009 11:15
in case your haystack is undefined or not set:
<?php
$fruitExists = in_array('mango', (array) $_SESSION["fruits"]);
?>
will return false if the haystack is not an array.
01-Sep-2009 08:34
Just an FYI for anyone encountering strange issues with in_array.
$a = array('edit','view','somethingelse',0);
echo in_array('edit',$a) ? 'true' : 'false';
echo in_array('delete',$a) ? 'true' : 'false';
This returns "true" for both checks. instead of false for the second check.
I encountered this while debugging an issue in mediawiki.. one of the addons I used defined the rights array incorrectly and added a "0" to the array so ALL RIGHTS CHECKS WERE TRUE!!!!.
http://bugs.php.net/bug.php?id=8015
this bug report basically brushes off the obvious wrong behaviour as "you must use strict checking"
11-Aug-2009 12:02
The function which Robin is proposing is known as an Iterative (as opposed to Recursive) Binary Search. It is indeed faster, but can only be used in situations when the haystack is in ascending order. If the haystack is not sorted it may return incorrect results. For this reason it cannot always be used as a direct replacement for in_array. For general purpose arrays, in_array is as good as it gets.
25-Jul-2009 07:38
This function is five times faster than in_array(). It uses a binary search and should be able to be used as a direct replacement:
<?php
function fast_in_array($elem, $array)
{
$top = sizeof($array) -1;
$bot = 0;
while($top >= $bot)
{
$p = floor(($top + $bot) / 2);
if ($array[$p] < $elem) $bot = $p + 1;
elseif ($array[$p] > $elem) $top = $p - 1;
else return TRUE;
}
return FALSE;
}
?>
23-Jul-2009 06:42
in_array() and array_search() are not identical. While trying to track down a bug, I discovered that this would produce a match:
<?php
$raw_data_item_types = array(0,5,9,'g','I');
$item="?gopher"
if(true===in_array($item, $raw_item_types))
{
// ...
}
?>
Obviously not the desired result! Replacing all instances of in_array with array_search() solved the immediate problem of not producing a match when there wasn't one to begin with.
01-Jul-2009 11:34
I just struggled for a while with this, although it may be obvious to others.
If you have an array with mixed type content such as:
<?php
$ary = array (
1,
"John",
0,
"Foo",
"Bar"
);
?>
be sure to use the strict checking when searching for a string in the array, or it will match on the 0 int in that array and give a true for all values of needle that are strings strings.
<?php
var_dump( in_array( 2, $ary ) );
// outputs FALSE
var_dump( in_array( 'Not in there', $ary ) );
// outputs TRUE
var_dump( in_array( 'Not in there', $ary, TRUE ) );
// outputs FALSE
?>
17-Apr-2009 05:46
This function will generate a PHP_NOTICE if you are looking for data of type A in an array containing data of type B if casting A to B would generate a PHP_NOTICE. This may not be obvious. For example:
<?php
$o = new stdClass;
$a = array(1, 2, $o);
in_array(5, $a);
?>
The output here is:
Notice: Object of class stdClass could not be converted to int in /some/script.php on line 5
09-Mar-2009 07:58
A little function to use an array of needles:
<?php
function array_in_array($needles, $haystack) {
foreach ($needles as $needle) {
if ( in_array($needle, $haystack) ) {
return true;
}
}
return false;
}
?>
08-Mar-2009 10:55
If made a in_array function that checks if the specified key matches. It works recursivly so it doesn't matter how deep your input array is.
<?php
function myInArray($array, $value, $key){
//loop through the array
foreach ($array as $val) {
//if $val is an array cal myInArray again with $val as array input
if(is_array($val)){
if(myInArray($val,$value,$key))
return true;
}
//else check if the given key has $value as value
else{
if($array[$key]==$value)
return true;
}
}
return false;
}
?>
28-Feb-2009 01:04
For a case-insensitive in_array(), you can use array_map() to avoid a foreach statement, e.g.:
<?php
function in_arrayi($needle, $haystack) {
return in_array(strtolower($needle), array_map('strtolower', $haystack));
}
?>
13-Feb-2009 12:38
Removes same text with in_array in a string.
<?
$hizmet="aeg,akai,aeg,arcelik,aeg,arcelik,klima,kombi";
// alots of same stings
$x=explode(",",$hizmet);
$t= array();
$k=0;
for($i=0;$i<sizeof($x);$i++){ // this for remove its
//echo $x[$i]."\n";
if(!in_array($x[$i],$t))
{
$t[$k]=$x[$i];
$k++;
}
}
for($i=0;$i<sizeof($t);$i++){ // rebuilding $hizmet strings.
echo $t[$i].",";
}
?>
28-Jan-2009 09:37
Here's a simple little function I wrote that is a case insensitive version of in_array():
<?php
function in_arrayi( $needle, $haystack ) {
$found = false;
foreach( $haystack as $value ) {
if( strtolower( $value ) == strtolower( $needle ) ) {
$found = true;
}
}
return $found;
}
?>
21-Jan-2009 03:54
function similar to in_array but implements LIKE '<string>%'
<?php
function in_array_like($referencia,$array){
foreach($array as $ref){
if (strstr($referencia,$ref)){
return true;
}
}
return false;
}
?>
17-Jan-2009 09:05
I found out that in_array will *not* find an associative array within a haystack of associative arrays in strict mode if the keys were not generated in the *same order*:
<?php
$needle = array(
'fruit'=>'banana', 'vegetable'=>'carrot'
);
$haystack = array(
array('vegetable'=>'carrot', 'fruit'=>'banana'),
array('fruit'=>'apple', 'vegetable'=>'celery')
);
echo in_array($needle, $haystack, true) ? 'true' : 'false';
// Output is 'false'
echo in_array($needle, $haystack) ? 'true' : 'false';
// Output is 'true'
?>
I had wrongly assumed the order of the items in an associative array were irrelevant, regardless of whether 'strict' is TRUE or FALSE: The order is irrelevant *only* if not in strict mode.
30-Oct-2008 05:17
Be aware of oddities when dealing with 0 (zero) values in an array...
This script:
<?php
$array = array('testing',0,'name');
var_dump($array);
//this will return true
var_dump(in_array('foo', $array));
//this will return false
var_dump(in_array('foo', $array, TRUE));
?>
It seems in non strict mode, the 0 value in the array is evaluating to boolean FALSE and in_array returns TRUE. Use strict mode to work around this peculiarity.
This only seems to occur when there is an integer 0 in the array. A string '0' will return FALSE for the first test above (at least in 5.2.6).
16-Sep-2008 09:54
Small method i built for my Array module, after looking through the manual I wanted a small compact way of making a wildcard search through an arrays values, and returning only those that it found.
<?php
/**
* Takes a needle and haystack (just like in_array()) and does a wildcard search on it's values.
*
* @param string $string Needle to find
* @param array $array Haystack to look through
* @result array Returns the elements that the $string was found in
*/
function find ($string, $array = array ())
{
foreach ($array as $key => $value) {
unset ($array[$key]);
if (strpos($value, $string) !== false) {
$array[$key] = $value;
}
}
return $array;
}
?>
02-Sep-2008 07:43
Here's a function that does an in_array, but takes wildcards in the needle, and also can be case sensitive/insensitive...
A few points:
-It doesn't use foreach, but for, which is quicker
-I didn't use regex to search with a wildcard for the reason that the needle could be unpredictable if it's user-input, and rather than having to escape metacharacters, I decided it would be easier to do a plain text comparison.
-Needles with wildcards can come in many forms such as:
Str*ng
S*r*ng*
*rng
*i*n*
so a split is being done on that string, and each part is then compared with the current item. If the first part is not found, the comparison is done, and we move on. If it IS found, we move on to the next part of the needle, while chopping off the initial part of the haystack string. This is to ensure that each comparison of a needle part is looking at the next part of the haystack string.
For example:
needle: "Bo*bo"
haystack[0] = "Bob is lazy"
On the first pass, when searching "Bo", we then modify the haystack[0] to be: "b is lazy" so that "bo" is compared with that. Otherwise, we'd be comparing "bo" with "Bob is lazy", and returning true incorrectly.
I haven't fully tested the function, so let me know if you spot any bugs, or have any questions.
<?php
function in_wildarray($needle, $haystack, $case_sensitive=true) {
$is_wild = (strpos($needle,"*")===true)? true : false;
$needles = ($is_wild)? explode("*", $needle) : array();
$needle = ($case_sensitive)? $needle : strtolower($needle);
for($i=0;$i<count($haystack);$i++) {
$haystack_str = ($case_sensitive)? haystack[$i] : strtolower($haystack[$i]);
if ($is_wild) {
$found = false;
for($x=0;$x<count($needles);$x++) {
$needle_part = trim($needles[x]);
$needle_index = strpos($haystack_str, $needle_part);
if ($needle_index===false) {
$found = false;
break; //break out of the loop, because string part is not found in the haystack string
} else {
$found = true;
//chop off the start of the string to the needle_index
//so we can be sure that the found items are in the correct order
//and we are avoiding the potential of finding duplicate characters
$haystack_str = substr($haystack_str, 0, $needle_index);
}
}
if ($found) { return true; }
} elseif (!$is_wild && $haystack_str == $needle) {
return true;
}
}
return false;
}
?>
As the code is right now, when there are wild cards, it will treat the initial segment as though it's preceded with a wild card. In other words, the first segment will be searched, not simply at the beginning of the string, but anywhere. Because it's a simple fix, I'll leave it to others. :)
21-Jul-2008 03:34
Recursive in array using SPL
<?php
function in_array_recursive($needle, $haystack) {
$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($haystack));
foreach($it AS $element) {
if($element == $needle) {
return true;
}
}
return false;
}
?>
19-May-2008 09:20
When using numbers as needle, it gets tricky:
Note this behaviour (3rd statement):
in_array(0, array(42)) = FALSE
in_array(0, array('42')) = FALSE
in_array(0, array('Foo')) = TRUE
in_array('0', array('Foo')) = FALSE
05-Mar-2008 11:43
A first idea for a function that checks if a text is in a specific column of an array.
It does not use in_array function because it doesn't check via columns.
Its a test, could be much better. Do not use it without test.
<?php
function in_array_column($text, $column, $array)
{
if (!empty($array) && is_array($array))
{
for ($i=0; $i < count($array); $i++)
{
if ($array[$i][$column]==$text || strcmp($array[$i][$column],$text)==0) return true;
}
}
return false;
}
?>
29-Jan-2008 07:52
In PHP 4, the first argument seems not allowed to be an object. In PHP 5, also objects are allowed as $needle.
16-Oct-2007 10:20
In case you have to check for unknown or dynamic variables in an array, you can use the following simple work-around to avoid misleading checks against empty and zero values (and only these "values"!):
<?php
in_array($value, $my_array, empty($value) && $value !== '0');
?>
The function empty() is the right choice as it turns to true for all 0, null and ''.
The '0' value (where empty() returns true as well) has to be excluded manually (as this is handled by in_array correctly!).
Examples:
<?php
$val = 0;
$res = in_array($val, array('2007'));
?>
leads incorrectly to true where
<?php
$val = 0;
$res = in_array($val, array('2007'), empty($val) && $val !== '0');
?>
leads correctly to false (strict check!) while
<?php
$val = 2007;
$res = in_array($val, array('2007'), empty($val) && $val !== '0');
?>
still correctly finds the '2007' ($res === true) because it ignores strict checking for that value.
02-Aug-2007 05:44
Be careful with checking for "zero" in arrays when you are not in strict mode.
in_array(0, array()) == true
in_array(0, array(), true) == false
21-May-2007 03:48
A simple function to type less when wanting to check if any one of many values is in a single array.
<?php
function array_in_array($needle, $haystack) {
//Make sure $needle is an array for foreach
if(!is_array($needle)) $needle = array($needle);
//For each value in $needle, return TRUE if in $haystack
foreach($needle as $pin)
if(in_array($pin, $haystack)) return TRUE;
//Return FALSE if none of the values from $needle are found in $haystack
return FALSE;
}
?>
16-Mar-2007 06:43
Be careful!
in_array(null, $some_array)
seems to differ between versions
with 5.1.2 it is false
but with 5.2.1 it's true!
04-Jun-2006 12:52
I needed a version of in_array() that supports wildcards in the haystack. Here it is:
<?php
function my_inArray($needle, $haystack) {
# this function allows wildcards in the array to be searched
foreach ($haystack as $value) {
if (true === fnmatch($value, $needle)) {
return true;
}
}
return false;
}
$haystack = array('*krapplack.de');
$needle = 'www.krapplack.de';
echo my_inArray($needle, $haystack); # outputs "true"
?>
Unfortunately, fnmatch() is not available on Windows or other non-POSIX compliant systems.
Cheers,
Thomas
09-Apr-2006 03:23
Here's another deep_in_array function, but this one has a case-insensitive option :)
<?
function deep_in_array($value, $array, $case_insensitive = false){
foreach($array as $item){
if(is_array($item)) $ret = deep_in_array($value, $item, $case_insensitive);
else $ret = ($case_insensitive) ? strtolower($item)==$value : $item==$value;
if($ret)return $ret;
}
return false;
}
?>
22-Feb-2006 03:11
Sorry, that deep_in_array() was a bit broken.
<?php
function deep_in_array($value, $array) {
foreach($array as $item) {
if(!is_array($item)) {
if ($item == $value) return true;
else continue;
}
if(in_array($value, $item)) return true;
else if(deep_in_array($value, $item)) return true;
}
return false;
}
?>
05-Feb-2006 02:52
Here's a gotcha, and another reason to always use strict with this function.
$x= array('this');
$test= in_array(0, $x);
var_dump($test); // true
$x= array(0);
$test= in_array('that', $x);
var_dump($test); // true
$x= array('0');
$test= in_array('that', $x);
var_dump($test); // false
It's hard to think of a reason to use this function *without* strict.
This is important for validating user input from a set of allowed values, such as from a <select> tag.
in_arrayr -- Checks if the value is in an array recursively
Description
bool in_array (mixed needle, array haystack)
<?php
function in_arrayr($needle, $haystack) {
foreach ($haystack as $v) {
if ($needle == $v) return true;
elseif (is_array($v)) return in_arrayr($needle, $v);
}
return false;
}
// i think it works
?>
08-Nov-2005 09:21
hope this function may be useful to you, it checks an array recursively (if an array has sub-array-levels) and also the keys, if wanted:
<?php
function rec_in_array($needle, $haystack, $alsokeys=false)
{
if(!is_array($haystack)) return false;
if(in_array($needle, $haystack) || ($alsokeys && in_array($needle, array_keys($haystack)) )) return true;
else {
foreach($haystack AS $element) {
$ret = rec_in_array($needle, $element, $alsokeys);
}
}
return $ret;
}
?>
03-Aug-2005 02:05
Beware of type conversion!
This snippet will unset every 0 key element form the array, when cycling an array which contains at least one _num value.
This is because php tries to convert every element of $forbidden_elements to integer when encountering a numeric index into array.
So $array[0] it's considered equal to (int)'_num'.
<?php
$forbidden_elements=array('_num');
foreach ($array as $key=>$value){
if (in_array($key,$forbidden_elements)){
unset ($array[$key]);
}
}
?>
The following example works, anway you can use strict comparison as well.
<?php
$forbidden_elements=array('_num');
foreach ($array as $key=>$value){
if (in_array($key,$forbidden_elements) && is_string($key)){
unset ($array[$key]);
}
}
?>
11-Jun-2005 12:26
If you have a multidimensional array filled only with Boolean values like me, you need to use 'strict', otherwise in_array() will return an unexpected result.
Example:
<?php
$error_arr = array('error_one' => FALSE, 'error_two' => FALSE, array('error_three' => FALSE, 'error_four' => FALSE));
if (in_array (TRUE, $error_arr)) {
echo 'An error occurred';
}
else {
echo 'No error occurred';
}
?>
This will return 'An error occurred' although theres no TRUE value inside the array in any dimension. With 'strict' the function will return the correct result 'No error occurred'.
Hope this helps somebody, cause it took me some time to figure this out.
08-Jan-2003 01:05
case-insensitive version of in_array:
<?php
function is_in_array($str, $array) {
return preg_grep('/^' . preg_quote($str, '/') . '$/i', $array);
}
?>
25-Nov-2002 02:56
if the needle is only a part of an element in the haystack, FALSE will be returned, though the difference maybe only a special char like line feeding (\n or \r).
10-Aug-2002 02:17
I searched the general mailing list and found that in PHP versions before 4.2.0 needle was not allowed to be an array.
Here's how I solved it to check if a value is in_array to avoid duplicates;
<?php
$myArray = array(array('p', 'h'), array('p', 'r'));
$newValue = "q";
$newInsert = array('p','q');
$itBeInThere = 0;
foreach ($myArray as $currentValue) {
if (in_array ($newValue, $currentValue)) {
$itBeInThere = 1;
}
if ($itBeInThere != 1) {
array_unshift ($myArray, $newInsert);
}
?>
07-May-2002 10:14
Sometimes, you might want to search values in array, that does not exist. In this case php will display nasty warning:
Wrong datatype for second argument in call to in_array() .
In this case, add a simple statement before the in_array function:
<?php
if (sizeof($arr_to_searchin) == 0 || !in_array($value, $arr_to_searchin)) { /*...*/ }
?>
In this case, the 1st statement will return true, omitting the 2nd one.
09-Apr-2002 03:53
If you want to search a multiple array for a value - you can use this function - which looks up the value in any of the arrays dimensions (like in_array() does in the first dimension).
Note that the speed is growing proportional with the size of the array - why in_array is best if you can determine where to look for the value.
Copy & paste this into your code...
<?php
function in_multi_array($needle, $haystack)
{
$in_multi_array = false;
if(in_array($needle, $haystack))
{
$in_multi_array = true;
}
else
{
for($i = 0; $i < sizeof($haystack); $i++)
{
if(is_array($haystack[$i]))
{
if(in_multi_array($needle, $haystack[$i]))
{
$in_multi_array = true;
break;
}
}
}
}
return $in_multi_array;
}
?>
