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

PHPz
Release: 2023-04-20 15:26:00
Original
465 people have browsed it

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!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!