Checking if an Element exists in a PHP Array

To check if an element exists in a PHP array, you can use the in_array($search, $array, $mode) function. The $search parameter specifies the element or value to search in the specified array and can be of a mixed type (string, integer, or other types). If the parameter is a string type, the search will be case-sensitive. The $array parameter specifies the array to search for the value. The $mode parameter is an optional boolean parameter that specifies the search mode. If the parameter is set to TRUE, then the in_array() function will search for a value with the same value type as specified in the "search" parameter. The default value for the "mode" parameter is FALSE. In this PHP Search In Array example, we use the in_array() function to check whether the given value exists in the array. Click Execute to run the PHP In Array Example online and see the result.
Checking if an Element exists in a PHP Array Execute
<?php
$name = array('Alice', 'Jack', 'Leo', 'Dora');

if (in_array('Jack', $name)){
  echo "Found";
}
else {
  echo "Not Found";
}
?>
Updated: Viewed: 6539 times

What is PHP?

PHP is a general-purpose server-side language for web development. PHP supports many protocols and standards, such as HTTP, SMTP, POP3, IMAP, FTP, and XML. PHP integrates with other web development technologies such as HTML, CSS, JavaScript, and SQL. Due to its open nature and huge developer community, PHP has many libraries and frameworks that simplify and speed up web application development. PHP is also a cross-platform language, allowing it to run on various operating systems such as Windows, macOS, Linux, and Unix.

What is an array in PHP?

An array in PHP is a variable we use to store data containing various elements. The elements of an array are identified by a unique key or index, which can be a number or a string. PHP has two types of arrays: indexed arrays and associative arrays. PHP provides many built-in functions for working with arrays, including searching, sorting, splitting a string to array, converting an array to string, converting an array to JSON, and getting the length of an array.

PHP In Array Syntax

The following is the syntax of the in_array() function:

PHP in_array() Syntax
in_array(search, array, mode)

Where:
  • search: specifies the element or value to look up in the given array
  • array: specifying an array to search for a value
  • mode (optional): specifies the mode to perform the search. If set to "TRUE", the in_array() function searches for the search-string and specific type in the array. The default value for this parameter is "FALSE".
PHP in_array() Example
<?php
$name = array('Alice', 'Jack', 'Leo', 'Dora');

if (in_array('Jack', $name)){
  echo "Found";
}
else {
  echo "Not found";
}
?>

#output: Found

PHP In Array Examples

The following are examples of checking if an element exists in a PHP array:

Checking if a element exists in an associative array

The following is an example of checking if a value exists in an associative array:

PHP
<?php
$arr = array(
  "name" => "Leo",
  "age" => 26,
  "city" => "Barcelona"
);

if (in_array("Leo", $arr)) {
  echo "Found";
} else {
  echo "Not found";
}
?>

#output: Found

Checking if an element exists in a multidimensional array

The following is an example of checking if a value exists in a multidimensional array:

PHP
<?php
$arr = array(
  array("name" => "Leo", "age" => 26),
  array("name" => "Alice", "age" => 32),
  array("name" => "Jack", "age" => 27)
);

if (in_array("Alice", array_column($arr, "name"))) {
  echo "Found";
} else {
  echo "Not found";
}
?>

#output: Found

Checking if an element exists in an array, case-insensitive

To check if an array contains an element, case insensitively, you can convert all elements in the array and the target element to lowercase (or uppercase) and then use the in_array() method to check for the presence of the target element:

PHP
<?php
$arr = array("cherry", "apple", "orange", "banana");
$target = "OrAnGE";

if (in_array(strtolower($target), array_map('strtolower', $arr))) {
  echo "Found";
} else {
  echo "Not Found";
}
?>

#output: Found

See also