Home Backend Development PHP Problem How to check whether the specified value is in the array in php

How to check whether the specified value is in the array in php

Apr 20, 2023 pm 03:06 PM

PHP is a widely used web programming language that has many features and utility functions. Among them, array is one of the most commonly used data types in PHP. When writing a web application, you may need to check whether a given value exists in an array.

In PHP, there are many ways to determine whether a value is in an array. Here are some commonly used methods.

  1. in_array() function

Using the in_array() function will check whether the given value exists in the array. The syntax of this function is as follows:

bool in_array ( mixed $value , array $array [, bool $strict = FALSE ] )

This function has three parameters. The first parameter is the value to look for, and the second parameter is the array in which to look for the value. The third optional parameter (defaults to FALSE) indicates whether types should be considered.

If the corresponding value is found, the function returns TRUE (Boolean value). Otherwise, the function returns FALSE (boolean).

For example, the following code demonstrates how to use the in_array() function to check whether a value is in an array.

$fruits = array("apple", "banana", "orange", "kiwi");
if (in_array("banana", $fruits)) {
  echo "Banana is found in the array";
} else {
  echo "Banana is not found in the array";
}
Copy after login

In this example, because the value "banana" can be found in the array $fruits, the function returns TRUE, and the final output is "Banana is found in the array".

  1. array_search() function

Use the array_search() function to return the key name (or index) of the value to be found in the array. The syntax of this function is as follows:

mixed array_search ( mixed $needle , array $haystack [, bool $strict = false ] )

This function has three parameters. The first parameter is the value to look for, and the second parameter is the array in which to look for the value. The third optional parameter (defaults to false) indicates whether types should be considered.

If the corresponding value is found, the function returns its key name or index. Otherwise, the function returns FALSE (boolean).

For example, the following code demonstrates how to use the array_search() function to find the key name of a value in an array.

$fruits = array("apple", "banana", "orange", "kiwi");
$key = array_search("orange", $fruits);
if ($key !== false) {
  echo "Orange is found in the array with key of " . $key;
} else {
  echo "Orange is not found in the array";
}
Copy after login

In this example, because the value "orange" can be found in the array $fruits, the function returns its key name (i.e. 2), and the final output is "Orange is found in the array with key of 2".

  1. isset() function

Use the isset() function to check whether a given key exists in an array. The syntax of this function is as follows:

bool isset (mixed $key, array $array)

This function has two parameters. The first parameter is the key to look for, and the second parameter is the array in which to look for the key.

If the corresponding key is found, the function returns TRUE (boolean). Otherwise, the function returns FALSE (boolean).

For example, the following code demonstrates how to use the isset() function to check whether a key exists in an array.

$fruits = array("apple", "banana", "orange", "kiwi");
if (isset($fruits[1])) {
  echo "The key 1 exists in the array";
} else {
  echo "The key 1 does not exist in the array";
}
Copy after login

In this example, because key 1 exists in the array $fruits, the function returns TRUE, and the final output is "The key 1 exists in the array".

  1. array_key_exists() function

Use the array_key_exists() function to check whether a given key exists in an array. The syntax of this function is as follows:

bool array_key_exists ( mixed $key , array $array )

This function has two parameters. The first parameter is the key to look for, and the second parameter is the array in which to look for the key.

If the corresponding key is found, the function returns TRUE (Boolean). Otherwise, the function returns FALSE (boolean).

For example, the following code demonstrates how to use the array_key_exists() function to check whether a key exists in an array.

$fruits = array("apple", "banana", "orange", "kiwi");
if (array_key_exists(3, $fruits)) {
  echo "The key 3 exists in the array";
} else {
  echo "The key 3 does not exist in the array";
}
Copy after login

In this example, because key 3 exists in the array $fruits, the function returns TRUE, and the final output is "The key 3 exists in the array".

To sum up, the above methods can be used to determine whether a value/key exists in a PHP array. You can choose which method to use based on your situation.

The above is the detailed content of How to check whether the specified value is in the array in php. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1252
24