略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: in_array

2024-11-14

in_array

(PHP 4, PHP 5, PHP 7, PHP 8)

in_array检查数组中是否存在某个值

说明

in_array(mixed $needle, array $haystack, bool $strict = false): bool

大海捞针,在大海(haystack)中搜索针( needle),如果没有设置 strict 则使用宽松的比较。

参数

needle

待搜索的值。

注意:

如果 needle 是字符串,则比较是区分大小写的。

haystack

待搜索的数组。

strict

如果第三个参数 strict 的值为 truein_array() 函数还会检查 needle类型是否和 haystack 中的相同。

返回值

如果找到 needle 则返回 true,否则返回 false

范例

示例 #1 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";
}
?>

第二个条件失败,因为 in_array() 是区分大小写的,所以以上程序显示为:

Got Irix

示例 #2 in_array() 严格类型检查例子

<?php
$a 
= array('1.10'12.41.13);

if (
in_array('12.4'$atrue)) {
    echo 
"'12.4' found with strict check\n";
}

if (
in_array(1.13$atrue)) {
    echo 
"1.13 found with strict check\n";
}
?>

以上例程会输出:

1.13 found with strict check

示例 #3 in_array() 中用数组作为 needle

<?php
$a 
= array(array('p''h'), array('p''r'), 'o');

if (
in_array(array('p''h'), $a)) {
    echo 
"'ph' was found\n";
}

if (
in_array(array('f''i'), $a)) {
    echo 
"'fi' was found\n";
}

if (
in_array('o'$a)) {
    echo 
"'o' was found\n";
}
?>

以上例程会输出:

  'ph' was found
  'o' was found

参见

  • array_search() - 在数组中搜索给定的值,如果成功则返回首个相应的键名
  • isset() - 检测变量是否已声明并且其值不为 null
  • array_key_exists() - 检查数组里是否有指定的键名或索引
add a noteadd a note

User Contributed Notes 39 notes

up
377
beingmrkenny at gmail dot com
10 years ago
Loose checking returns some crazy, counter-intuitive results when used with certain arrays. It is completely correct behaviour, due to PHP's leniency on variable types, but in "real-life" is almost useless.

The solution is to use the strict checking option.

<?php

// Example array

$array = array(
   
'egg' => true,
   
'cheese' => false,
   
'hair' => 765,
   
'goblins' => null,
   
'ogres' => 'no ogres allowed in this array'
);

// Loose checking -- return values are in comments

// First three make sense, last four do not

in_array(null, $array); // true
in_array(false, $array); // true
in_array(765, $array); // true
in_array(763, $array); // true
in_array('egg', $array); // true
in_array('hhh', $array); // true
in_array(array(), $array); // true

// Strict checking

in_array(null, $array, true); // true
in_array(false, $array, true); // true
in_array(765, $array, true); // true
in_array(763, $array, true); // false
in_array('egg', $array, true); // false
in_array('hhh', $array, true); // false
in_array(array(), $array, true); // false

?>
up
176
jv at vip dot ie
12 years ago
If you're working with very large 2 dimensional arrays (eg 20,000+ elements) it's much faster to do this...

<?php
$needle
= 'test for this';

$flipped_haystack = array_flip($haystack);

if ( isset(
$flipped_haystack[$needle]) )
{
  print
"Yes it's there!";
}
?>

I had a script that went from 30+ seconds down to 2 seconds (when hunting through a 50,000 element array 50,000 times).

Remember to only flip it once at the beginning of your code though!
up
8
davide.iandoli at fattura24.com
7 months ago
I tried to use in_array in combination with array_colum, here below a sample:

<?php
$customArray
= [
array(
'name' => 'test',
         
'is_gift' => true,
         
'reduction' => 10.0),
array(
'name' => 'test2',
         
'is_gift' => false,
         
'reduction' => 5.0)
];

$is_gift = in_array(true, array_column($customArray, 'is_gift'));

var_dump($is_gift);
?>

Result:
bool(true)

It seems to work right
up
26
Camille Dugas
2 years ago
<?php

class Method {

   
/** @var int current number of inMultiArray() loop */
   
private static $currentMultiArrayExec = 0;

   
/**
     * Checks if an element is found in an array or one of its subarray.
     * The verification layer of this method is limited to the parameter boundary xdebug.max_nesting_level of your php.ini.
     * @param mixed $element
     * @param array $array
     * @param bool $strict
     * @return bool
     */
   
public static function inMultiArray($element, array $array, bool $strict = true) : bool {
       
self::$currentMultiArrayExec++;

        if(
self::$currentMultiArrayExec >= ini_get("xdebug.max_nesting_level")) return false;

        foreach(
$array as $key => $value){
           
$bool = $strict ? $element === $key : $element == $key;

            if(
$bool) return true;

            if(
is_array($value)){
               
$bool = self::inMultiArray($element, $value, $strict);
            } else {
               
$bool = $strict ? $element === $value : $element == $value;
            }

            if(
$bool) return true;
        }

       
self::$currentMultiArrayExec = 0;
        return isset(
$bool) ? $bool : false;
    }
}

$array = array("foo" => array("foo2", "bar"));
$search = "foo";

if(
Method::inMultiArray($search, $array, false)){
    echo
$search . " it is found in the array or one of its sub array.";
} else {
    echo
$search . " was not found.";
}

# foo it is found in the array or one of its sub array.
up
74
Kelvin J
13 years ago
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));
    }
?>
up
27
Lea Hayes
10 years ago
Determine whether an object field matches needle.

Usage Example:
---------------

<?php
$arr
= array( new stdClass(), new stdClass() );
$arr[0]->colour = 'red';
$arr[1]->colour = 'green';
$arr[1]->state  = 'enabled';

if (
in_array_field('red', 'colour', $arr))
   echo
'Item exists with colour red.';
if (
in_array_field('magenta', 'colour', $arr))
   echo
'Item exists with colour magenta.';
if (
in_array_field('enabled', 'state', $arr))
   echo
'Item exists with enabled state.';
?>

Output:
--------
Item exists with colour red.
Item exists with enabled state.

<?php
function in_array_field($needle, $needle_field, $haystack, $strict = false) {
    if (
$strict) {
        foreach (
$haystack as $item)
            if (isset(
$item->$needle_field) && $item->$needle_field === $needle)
                return
true;
    }
    else {
        foreach (
$haystack as $item)
            if (isset(
$item->$needle_field) && $item->$needle_field == $needle)
                return
true;
    }
    return
false;
}
?>
up
5
artem dot g dot prozorov at gmail dot com
2 years ago
in_array() may also return NULL if the second argument is NULL and strict types are off.

<?php
var_dump
(in_array(1, null));
?>

The output is NULL

If the strict mode is on, then this code would end up with the TypeError
up
9
what loose checking means
8 years ago
In a high-voted example, an array is given that contains, amongst other things, true, false and null, against which various variables are tested using in_array and loose checking.

It impossible to receive false as a return value from in_array using loose checking if your arrays contains both the constants true and false. You might understandably trip over this (or the inverse - passing boolean true to check against an array of e.g. non-empty strings), but it's certainly not counter intuitive and makes perfect sense.
up
1
roman dot varuta at gmail dot com
3 years ago
If array contain at least one true value, in_array() will return true every times if it is not false or null

Use strict = true

<?php

$arr
= array(true);

in_array($any, $arr); // Will return true every time except null and false

?>
up
2
Carmen
4 years ago
Esta función falla con las letras acentuadas y con las eñes. Por tanto, no sirve para los caracteres UTF-8.
El siguiente código falla para na cadena = "María Mañas", no reconoce ni la "í" ni la "ñ":

    function validarNombreYApellidos ($cadena, $selector)
    {
        /* Se admiten las letras (he puesto sólo las mayusculas,
             porque paso la cadena con el nombre o
             el apellido a mayúscula antes de hacer la comparación),
             las vocales acentuadas, la diéresis,
             las eñes, los espacios en blanco y el guión
            (para los apellidos compuestos)*/
        $caracteresPermitidos =
               array ("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
                          "N", "Ñ", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X",
                          "Y",  "Z", " ", "Á", "É", "Í", "Ó", "Ú", "Ü", "-");
       
        $correcto = true;

        // ¿La cadena está vacía?
        if (empty ($cadena))
        {
            $correcto = false;
        }
        else
        {
            $nombreOapellido = mb_strtoupper ($cadena, "utf-8");
            $longitudCadena = mb_strlen ($cadena, "utf-8");

            for ($i = 0; ($i < $longitudCadena) && $correcto; $i++)
            {
                if (!in_array ($nombreOapellido [$i],
                                     $caracteresPermitidos))
                {
                    // Se ha encontrado un carácter no permitido
                    $correcto = false;
                }
            }
        }
            return $correcto;
     }
up
5
dazero0 dot ls at gmail dot com
6 years ago
I would like to add something to beingmrkenny at gmail dot com comparison post. After debugging a system, i discovered a security issue in our system and his post helped me find the problem.

In my additional testing i found out that not matter what you search for in an array, except for 0 and null, you get true as the result if the array contains true as the value.

Examples as php code :

<?php
$a
= ['a', 32, true, 'x' => y];
var_dump(in_array(25, $a)); // true, one would expect false
var_dump(in_array('ggg', $a)); // true, one would expect false

var_dump(in_array(0, $a)); // false
var_dump(in_array(null, $a)); // false
?>

Such the best practice in our case is to use strict mode. Which was not so obvious.
up
3
rhuan at rhuan dot com dot br
5 years ago
This code will search for a value in a multidimensional array with strings or numbers on keys.

function in_multiarray($elem, $array)
{
    while (current($array) !== false) {
        if (current($array) == $elem) {
            return true;
        } elseif (is_array(current($array))) {
            if (in_multiarray($elem, current($array))) {
                return true;
            }
        }
        next($array);
    }
    return false;
}
up
6
ctulek at gmail dot com
11 years ago
If you have an array like:
$arr = array(0,1,2,3,4,5);

in_array(NULL, $arr) returns true because you have 0 in your array. That is, in_array does not use === for equal check.
up
11
bogdan AT bogdanconstantinescu DOT com
12 years ago
If you found yourself in need of a multidimensional array in_array like function you can use the one below. Works in a fair amount of time

<?php

   
function in_multiarray($elem, $array)
    {
       
$top = sizeof($array) - 1;
       
$bottom = 0;
        while(
$bottom <= $top)
        {
            if(
$array[$bottom] == $elem)
                return
true;
            else
                if(
is_array($array[$bottom]))
                    if(
in_multiarray($elem, ($array[$bottom])))
                        return
true;
                   
           
$bottom++;
        }       
        return
false;
    }
?>
up
1
stocki dot r at gmail dot com
5 years ago
Add an extra if() to adrian foeder's comment to make it work properly:

<?php
   
...
    if (!@
$ret) {
       
$ret = rec_in_array($needle, $element, $alsokeys);
    }
    ...
?>

So this will work, too:

<?php
    $array
= array(
        array(
'a', 'b'),
        array(
'c', 'd')
    );
   
var_dump(rec_in_array('a', $array));
?>
up
4
rhill at xenu-directory dot net
13 years ago
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.
up
1
Harry Willis
7 years ago
Kelvin's case-insensitive in_arrayi is fine if you desire loose typing, but mapping strtolower onto the array will (attempt to) cast all array members to string. If you have an array of mixed types, and you wish to preserve the typing, the following will work:

<?php
function in_array_ci($needle, array $haystack, $strict = false) {
    foreach (
$haystack as $element) {
        if (
gettype($needle) == 'string' && gettype($element) == 'string') {
            if (!
strcasecmp($needle, $element)) {
                return
true;
            }
        }
        elseif (
$strict) {
            if (
$needle === $element) {
                return
true;
            }
        }
        else {
            if (
$needle == $element) {
                return
true;
            }
        }
    }

    return
false;
}
?>
up
2
john at dwarven dot co dot uk
12 years ago
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

?>
up
3
janis dot janovskis at gmail dot com
10 years ago
Since sometimes in_array returns strange results - see notes above.
I was able to find value in array by this quite a simple function;
<?php
/**
* $find <mixed> value to find
* $array<array> array to search in
*/

function _value_in_array($array, $find){
$exists = FALSE;
if(!
is_array($array)){
   return;
}
foreach (
$array as $key => $value) {
  if(
$find == $value){
      
$exists = TRUE;
  }
}
  return
$exists;
}

// Note
// You can't use wildcards and it does not check variable type
?>
up
3
crashrox at gmail dot com
13 years ago
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;
}
?>
up
2
Valerchik
9 years ago
Beware when using this function to validate user input:

$a = array('0' => 'Opt 1', '1' => 'Opt 2', '2' => 'Opt 3');
$v = 'sql injection';
var_dump(in_array($v, array_keys($a)));

This will result : true;

array_keys($a) will cast array keys to int instead of string !
then when in_array will compare it will cast  'sql injection' to int 0 !
Beware of this!
up
2
thomas dot sahlin at gmail dot com
12 years ago
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!');

?>
up
0
reiji_nakama at yahoo dot com
1 year ago
The top voted notes talked about creating strict comparison function, because in_array is insufficient, because it has very lenient type checking (which is PHP default behaviour).

The thing is, in_array is already sufficient. Because as a good programmer, you should never have an array which contains {true, false, 0, 1, '0', '1', null, '', 'hello world'}; all in one array anyway.

It's better to fix how you store data and retrieve data from user, rather than fixing in_array() which is not broken.
up
0
fermar21 at gmail dot com
2 years ago
If you need to find if a value in an array is in another array you can use the function:

<?php
function array_in_array($arr_needle,$arr_haystack){
   
$retorno = false;
    foreach (
$arr_needle as $value) {
        if(
in_array($value, $arr_haystack)){
           
$retorno = true;
        }
    }
    return
$retorno;
}
?>
up
0
msegit post pl
3 years ago
You can use my function inArray, to search:
- multidimensional arrays
- for substrings (case [in]sensitive)
- for sub-arrays
- get array of keys of found values
Look at github https://gist.github.com/msegu/80093a65316ded5b69558d5456f80ff9 (here is too long)
up
0
Geoffrey Hoffman
4 years ago
Be careful to use the strict parameter with truth comparisons of specific strings like "false":

<?php

$truthy
= [true, 'true', 1, '1', 'y', 'Y', 'yes', 'YES'];

if (
in_array('false', $truthy)) {
   echo
"False is truthy.\n";
} else {
   echo
"False is not truthy.\n";
}

if (
in_array('false', $truthy, true)) {
    echo
"False is truthy.\n";
} else {
    echo
"False is not truthy.\n";
}

?>

The above example prints:

False is truthy.
False is not truthy.
up
1
sick949 at hotmail dot com
14 years ago
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;
}

?>
up
0
tjamadeira at gmail dot com
6 years ago
This function is for search a needle in a multidimensional haystack:

<?php
/**
* A special function for search in a multidimensional array a needle
*
* @param mixed needle The searched variable
* @param array haystack The array where search
* @param bool strict It is or it isn't a strict search?
*
* @return bool
**/
function in_array_r($needle, $haystack, $strict = false){
foreach(
$haystack as $item){
   if(
is_array($item)){
     if(
in_array_r($needle, $item, $strict)){
       return
true;
     }
   }else{
     if((
$strict ? $needle === $item : $needle == $item)){
       return
true;
     }
   }
}
return
false;
}
?>
up
1
Martijn Wieringa
14 years ago
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
up
0
Aragorn5551 at gmx dot de
17 years ago
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.
up
-2
rolf dot dergham dot public at gmail dot com
8 years ago
Watch out for this:

<?

print_r( (int) in_array('hello',array( 0 => 0)) );

?>

returns 1

Yes, it seems that is_array thinks that a random string and 0 are the same thing.
Excuse me, that's not loose checking, that's drunken logic.
Or maybe I found a bug?
up
-1
splogamurugan at gmail dot com
6 years ago
var_dump(in_array('invalid', array(0,10,20)));
The above code gives true since the 'invalid' is getting converted to 0 and checked against the array(0,10,20)

but var_dump(in_array('invalid', array(10,20)));  gives 'false' since 0 not there in the array
up
-1
adrian foeder
16 years ago
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;
    }
?>
up
-3
hoopyfroop at yahoo dot com
5 years ago
If you search for numbers, in_array will convert any strings in your array to numbers, dropping any letters/characters, forcing a numbers-to-numbers comparison. So if you search for 1234, it will say that '1234abcd' is a match.  Example:

<?php
$test_array
= array('test', '1234abcd');
if (
in_array(1234, $test_array)) {
    echo
'1234 is a match!';
}
?>
up
-3
musik at krapplack dot de
16 years ago
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
up
-3
Carmen
4 years ago
Esta función falla con las letras acentuadas y con las eñes. Por tanto, no sirve para los caracteres UTF-8.
El siguiente código falla para na cadena = "María Mañas", no reconoce ni la "í" ni la "ñ":

    function validarNombreYApellidos ($cadena, $selector)
    {
        /* Se admiten las letras (he puesto sólo las mayusculas,
             porque paso la cadena con el nombre o
             el apellido a mayúscula antes de hacer la comparación),
             las vocales acentuadas, la diéresis,
             las eñes, los espacios en blanco y el guión
            (para los apellidos compuestos)*/
        $caracteresPermitidos =
               array ("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
                          "N", "Ñ", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X",
                          "Y",  "Z", " ", "Á", "É", "Í", "Ó", "Ú", "Ü", "-");
       
        $correcto = true;

        // ¿La cadena está vacía?
        if (empty ($cadena))
        {
            $correcto = false;
        }
        else
        {
            $nombreOapellido = mb_strtoupper ($cadena, "utf-8");
            $longitudCadena = mb_strlen ($cadena, "utf-8");

            for ($i = 0; ($i < $longitudCadena) && $correcto; $i++)
            {
                if (!in_array ($nombreOapellido [$i],
                                     $caracteresPermitidos))
                {
                    // Se ha encontrado un carácter no permitido
                    $correcto = false;
                }
            }
        }
            return $correcto;
     }
up
-2
iammanjil at gmail dot com
3 years ago
If third parameter is not set to Strict then, the needle is found in haystack eventhought the values are not same. the limit behind the decimal seems to be 6 after which, the haystack and needle match no matter what is behind the 6th.

Wrong Response.
$os = array("652875063.10089021");
if (in_array("652875063.10089021", $os)) {
    echo "In Array";
}

Correct Response
$os = array("652875063.1008902");
if (in_array("652875063.1008901", $os)) {
    echo "In Array";
}
up
-3
ethraza at gmail dot com
1 year ago
A function to check an array of values within another array.

<?php
/**
* Return TRUE if any value in neddles exists in haystack (values or keys) or FALSE.
*/
function array_in_array_exists($aNeedles, $aHaystack_values, $aHaystack_keys = null) {
   
$needles = (is_string($aNeedles))? explode(',', $aNeedles) : $aNeedles;

    if (
is_array($aHaystack_values)) {
       
$search_func = 'in_array';
       
$haystack = $aHaystack_values;
    } else {   
       
$search_func = 'array_key_exists';
       
$haystack = $aHaystack_Keys;
    }

    return !empty(
array_filter($needles, function($e) use ($search_func, $haystack) {
            return
call_user_func($search_func, $e, $haystack);
    } ));
}

// Usage example:
var_dump( array_in_array_exists(array('000', '123'), array('123','456')) );
?>
bool(true)

Second element '123' of needles was found as first element of haystack, so it return TRUE.
up
-8
loordu
2 years ago
In PHP array function the in_array() function mainly used to check the item are available or not in array.

In_array() Syntax:

in_array( 'search_item' , $array, boolean)

Where,
     boolean - It is used to check strict validation. And this field is optional one. If you want the strict validation you can use this.

We using in_array() function to performing following following two function. That is ,

1. Non-strict validation
2. Strict validation

1. Non-strict validation:
     This method to validate array with some negotiation. And it allow two parameters.

     For example,
          $array_sample = array (  'key1' => 567 , 'key2' => 579);

          in_array('key1', $array_sample);   // output1:  true
          in_array('577', $array_sample, false);   // output2:  true

      Note: the Example 1, we use only two parameter. Because we can't mention `false` value. Because In default the in_array() take `false` as a boolean value.

      In above example,
             Example 1 : The `key1` is not value in the array.    This is key of the array. So this scenario the in_array accept the search key as a value of the array.
             Example 2: The value `577` is not in the value and key of the array. It is some similar to the value `579`. So this is also accepted.

     So this reason this type is called non-strict function.

2. Strict validation
     This method to validate array without any negotiation. And it have three parameters. If you only mention two parameter the `in_array()` function take as a non-strict validation.

     For example,
          $array_sample = array (  'key1' => 567 , 'key2' => 579);

          in_array('key1', $array_sample , true);   // output1:  false
          in_array('577', $array_sample, true);   // output2:  false
       
    This is return `true` only the search string is match exactly with the array value with case sensitivity.

Thanks for your time.

官方地址:https://www.php.net/manual/en/function.in-array.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3