PHP Array in_array


The PHP in_array() function checks for the presence ofavalue in the array, returns TRUE if it exists, and FALSE otherwise.

grammar:

bool in_array(mixed needle, array array [, bool strict])

parameter Description
needle The value to be searched for in the array, or case-sensitive if it is a string
array The array to be retrieved
strict Optional, if set to TRUE, the value of needle and array types will also be checked

example:

$arr_a = array("a", "b", " c ", 1);
if (in_array("a", $arr_a)) {
    echo 'characterain the $arr_a array exists';
} else {
    echo 'characterain the $arr_a array does not exist';
}

Example output results are as follows:

The characterais present in the $arr_a array

Examples of strict inspection:


$arr_a = array("a", "b", " c ", 1);
if (in_array(" 1 ", $arr_a, TRUE)) {
    echo 'character 1 exists in the $arr_a array';
} else {
    echo 'character 1 does not exist in the $arr_a array';
}

Example output results are as follows:

The character 1 does not exist in the $arr_a array

array as an example of needle:

$arr_a = array("a", "b"), 1, 2);
$arr_b = array("a", "b");
if (in_array($arr_b, $arr_a)) {
  echo 'The array $arr_b exists in the $arr_a array';
} else {
  echo 'The array $arr_b not exists in the $arr_a array';
}

Example output results are as follows:

The array $arr_b exists in the $arr_a array



Copyright 2014-2017 by DeveloperTut.com. All Rights Reserved.
TwitterGoogle+Facebook