PHP in_array() Function

In this chapter you will learn:

  1. Definition for PHP in_array() Function
  2. Syntax for PHP in_array() Function
  3. Parameter for PHP in_array() Function
  4. Example - Is an element in an array
  5. Example - Use strict checking

Definition

The in_array() function returns true if an array contains a specific value; otherwise, it will return false:

Syntax

PHP in_array() Function has the following syntax.

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

Parameter

ParameterIs RequiredDescription
searchRequired.What to search for
arrayRequired.Array to search
typeOptional.Use strict checking

The optional boolean third parameter for in_array() (set to false by default) defines whether to use strict checking.

If parameter three is set to true, PHP will the === operator to do the search.

Example 1

Is an element in an array


<?PHP/* j  a  v  a  2  s . c om*/
$needle = "java2s.com";
$haystack = array("A", "java2s.com", "B", "C", "D", "E");

if (in_array($needle, $haystack)) {
   print "$needle is in the array!\n";
} else {
   print "$needle is not in the array\n";
}
?>

The code above generates the following result.

Example 2

Use strict checking


<?PHP//from  j a va 2s.  c  om
$needle = 5;
$haystack = array("5", "java2s.com", "B", "C", "D", "E");

if (in_array($needle, $haystack,true)) {
   print "$needle is in the array!\n";
} else {
   print "$needle is not in the array\n";
}

if (in_array($needle, $haystack,TRUE)) {
   print "$needle is in the array!\n";
} else {
   print "$needle is not in the array\n";
}
?>

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. Definition for PHP key() Function
  2. Syntax for PHP key() Function
  3. Parameter for PHP key() Function
  4. Return value for PHP key() Function
  5. Example - Return the element key from the current internal pointer position
Home » PHP Tutorial » PHP Array Functions
PHP array() function
PHP array_change_key_case() function
PHP array_chunk() function
PHP array_combine() function
PHP array_count_values() function
PHP array_diff() function
PHP array_diff_assoc() Function
PHP array_diff_key() function
PHP array_diff_uassoc() function
PHP array_diff_ukey() function
PHP array_fill() function
PHP array_fill_keys() function
PHP array_filter() function
PHP array_flip() function
PHP array_intersect() Function
PHP array_intersect_assoc() Function
PHP array_intersect_key() function
PHP array_intersect_uassoc() Function
PHP array_intersect_ukey() Function
PHP array_key_exists() Function
PHP array_keys() function
PHP array_map() Function
PHP array_merge() function
PHP array_merge_recursive() Function
PHP array_multisort() Function
PHP array_pad() Function
PHP array_pop() function
PHP array_product() Function
PHP array_push() function
PHP array_rand() function
PHP array_reduce() Function
PHP array_replace() Function
PHP array_replace_recursive() Function
PHP array_reverse() Function
PHP array_search() Function
PHP array_shift() Function
PHP array_slice() Function
PHP array_splice() Function
PHP array_sum() Function
PHP array_udiff() Function
PHP array_udiff_assoc() Function
PHP array_udiff_uassoc() Function
PHP array_uintersect() Function
PHP array_uintersect_assoc() Function
PHP array_uintersect_uassoc() Function
PHP array_unique() Function
PHP array_unshift() Function
PHP array_values() Function
PHP array_walk() Function
PHP array_walk_recursive() Function
PHP arsort() Function
PHP asort() Function
PHP compact() Function
PHP count() Function
PHP current() Function
PHP each() Function
PHP end() Function
PHP explode() Function
PHP extract() Function
PHP implode() Function
PHP in_array() Function
PHP key() Function
PHP krsort() Function
PHP ksort() Function
PHP list() Function
PHP natcasesort() Function
PHP natsort() Function
PHP next() Function
PHP pos() Function
PHP prev() Function
PHP range() Function
PHP reset() Function
PHP rsort() Function
PHP shuffle() Function
PHP sizeof() Function
PHP sort() Function
PHP uasort() Function
PHP uksort() Function
PHP usort() Function