English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Basic PHP Tutorial

Advanced PHP Tutorial

PHP & MySQL

PHP Reference Manual

Usage and examples of PHP in_array() function

PHP Array Function Manual

The in_array() function checks if a specified value exists in an array

Syntax

in_array ( $value, $array [,$strict] );

Definition and usage

The in_array() function searches for a specific value in an array. If the third parameterstrictSet to TRUE, then the in_array() function will also check the type of $value.

Note:If value is a string, the comparison is case-sensitive.

Parameter

Serial numberParameters and descriptions
1

value (required)

The value to be searched in the array.

2

array (required)

It specifies an array

3

strict (optional)

If the value of the third parameter strict is TRUE, the in_array() function will also check if the type of value is the same as that in the array.

Return value

If the value is found in the array, this function returns TRUE, otherwise it returns FALSE.

Online example: Find string

Find a specified string in the array

<?php
   $mobile_os = array("Mac", "android", "java", "Linux");
   
   if (in_array("java", $mobile_os)) {
      echo "Got java";
   }
   
   if (in_array("mac", $mobile_os)) {
      echo "Got mac";
   }
?>
Test and see ‹/›

Output result:

Got java

The first condition is successful, returns true and outputs the result; while the second condition fails because in_array() is case-sensitive, and there is no lowercase 'mac' in the array.

Online example: Find array

Find another array in the array

<?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";
}
?>
Test and see ‹/›

Output result:

'ph' was found
'o' was found

  PHP Array Function Manual