PHP determines which array the value is in

WBOY
Release: 2023-05-05 14:43:50
Original
512 people have browsed it

In PHP programming, it is often necessary to determine whether a value exists in an array. If the value exists in the array, you usually need to obtain the position or key name of the value in the array. This article will introduce how to determine whether a value exists in an array in PHP, and obtain the position or key name of the value in the array.

1. PHP in_array() function

PHP provides an in_array() function to determine whether a value exists in an array. The syntax of this function is as follows:

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

Among them, $needle represents the value to be found, and $haystack represents The array to be searched, $strict indicates whether strict mode is turned on, the default is false, that is, strict mode is not turned on. After turning on strict mode, you need to compare values ​​and types at the same time.

The return value of this function is a Boolean value, that is, if $needle exists in $haystack, it returns true, otherwise it returns false.

Example 1: Check if an integer exists in an array

$numbers = array(1, 2, 3, 4, 5);
if (in_array(3, $numbers)) {

echo "3 is found in the array";
Copy after login

} else {

echo "3 is not found in the array";
Copy after login

}
?>

The output result is: 3 is found in the array

Example 2: Check if a string exists in an array

$fruits = array("apple", "banana", "orange", "pear");
if (in_array("orange", $fruits)) {

echo "orange is found in the array";
Copy after login

} else {

echo "orange is not found in the array";
Copy after login

}
?>

The output result is: orange is found in the array

2. PHP array_search() function

In some cases, it is necessary to obtain the position or key name of a value in the array. PHP provides an array_search() function to obtain the position or key name of a value in an array. The syntax of this function is as follows:

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

Among them, $needle represents the value to be found, and $haystack represents The array to be searched, $strict indicates whether strict mode is turned on, the default is false, that is, strict mode is not turned on. After turning on strict mode, you need to compare values ​​and types at the same time.

The return value of this function is the found key name. If it is not found, it returns false.

Example 1: Get the position of an integer in an array

$numbers = array(1, 2, 3, 4, 5);
$ key = array_search(3, $numbers);
echo "3 is found at position " . $key;
?>

The output result is: 3 is found at position 2

Example 2: Get the key name of a string in the array

$fruits = array("apple" => "red", "banana" => "yellow", "orange" => "orange", "pear" => "green");
$key = array_search("orange", $fruits);
echo "orange is found with key " . $key;
?>

The output result is: orange is found with key orange

3. PHP array_keys() function

If you need to obtain For all keys in an array that contain a certain value, you can use PHP's array_keys() function. The syntax of this function is as follows:

array array_keys ( array $array , mixed $search_value [, bool $strict = FALSE ] )

Among them, $array represents the array to be searched, and $search_value represents The value to be found, $strict indicates whether strict mode is enabled. The default is false, which means strict mode is not enabled. After turning on strict mode, you need to compare values ​​and types at the same time.

The return value of this function is an array containing all key names that meet the conditions.

Example 1: Get all the positions of an integer in the array

$numbers = array(1, 2, 3, 4, 5, 3, 6) ;
$keys = array_keys($numbers, 3);
echo "The value 3 is found at positions: ";
foreach ($keys as $key) {

echo $key . " ";
Copy after login

}
?>

The output result is: The value 3 is found at positions: 2 5

Example 2: Get all the key names of a string in the array

$fruits = array("apple" => "red", "banana" => "yellow", "orange" => "orange", "pear" => "green", "mango" => "yellow");
$keys = array_keys($fruits, "yellow");
echo "The value yellow is found with keys: ";
foreach ($keys as $key) {

echo $key . " ";
Copy after login

}
?>

The output result is: The value yellow is found with keys: banana mango

4. Conclusion

PHP provides a variety of methods to determine whether a value exists in an array and obtain the position or key name of the value in the array. Use the in_array() function to easily determine whether a value exists in an array; use the array_search() function to get the position or key name of a value in the array; use the array_keys() function to get all the values ​​in an array that contain a certain value. key name. Depending on actual needs, different methods can be used to complete the task more conveniently.

The above is the detailed content of PHP determines which array the value is in. 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!