Home  >  Article  >  Backend Development  >  How to determine whether an array contains a certain element in PHP (5 methods)

How to determine whether an array contains a certain element in PHP (5 methods)

PHPz
PHPzOriginal
2023-04-18 14:10:334549browse

Determining whether an array contains a specific element may be a very common task in PHP. In this article, I will explain several ways to determine whether an array contains an element using PHP. These methods can help you solve this problem easily, whether you are a newbie or an experienced PHP developer.

I will start with the most basic methods and then gradually introduce more advanced techniques. Depending on your experience and personal preference, you are free to choose the method that works for you.

Method 1: Use the in_array() function

The most commonly used method when determining whether an array contains an element is to use the in_array() function. This function is very simple and requires only two parameters: the element to be found and the array to be found.

The following is a sample code using the in_array() function:

$arr = array('apple', 'banana', 'orange', 'grape');
if (in_array('apple', $arr)) {
   echo '数组包含apple';
} else {
   echo '数组不包含apple';
}

If the array contains the element to be found, "array contains apple" will be output.

Method 2: Use the array_search() function

Another commonly used method is to use the array_search() function. This function is similar to the in_array() function, but the difference is that it not only returns true or false, but the key of the element in the array. If the element is not in the array, returns false.

The following is a sample code using the array_search() function:

$arr = array('apple', 'banana', 'orange', 'grape');
$search = array_search('apple', $arr);
if ($search !== FALSE) {
   echo '数组包含apple,位置为:'.$search;
} else {
   echo '数组不包含apple';
}

If the array contains the element to be found, "array contains apple, position: 0" will be output, because 'apple ' is the first element in the array.

Method 3: Use the in_array() and array_keys() functions

Combining the in_array() and array_keys() functions is also a possible method. The array_keys() function returns the values ​​of all keys in an array.

The following is a sample code using the in_array() and array_keys() functions:

$arr = array('apple', 'banana', 'orange', 'grape');
if (in_array('apple', $arr)) {
   $keys = array_keys($arr, 'apple');
   echo '数组包含apple,位置为:'.$keys[0];
} else {
   echo '数组不包含apple';
}

If the array contains the element you want to find, it will output "The array contains apple, position: 0" .

Method 4: Using foreach loop

Using foreach loop to traverse the array and find the specified element is also a possible method.

The following is a sample code using a foreach loop:

$arr = array('apple', 'banana', 'orange', 'grape');
$bFound = false;
foreach ($arr as $key => $value) {
  if ($value == 'apple') {
  echo '数组包含apple,位置为:'.$key;
  $bFound = true;
  break;
}
}
if (!$bFound) {
  echo '数组不包含apple';
}

If the array contains the element to be found, "The array contains apple, position: 0" will be output.

Method Five: Use the array_reduce() function

Finally, I will introduce a method of using the array_reduce() function. This function "reduces" all elements in the array using a callback function, and ultimately returns a value. Typically, we use this function to sum, multiply, or compute some kind of accumulator over numeric arrays.

But here, we can use it to determine whether the array contains a specific element. The following is a sample code using the array_reduce() function:

$arr = array('apple', 'banana', 'orange', 'grape');
$found = array_reduce($arr, function($carry, $item){
   return $carry || $item == 'apple';
}, false);
if ($found) {
   echo '数组包含apple';
} else {
   echo '数组不包含apple';
}

If the array contains the element you are looking for, "array contains apple" will be output.

Conclusion

No matter which method is used, as long as you master the basic PHP syntax and array concepts, you can easily determine whether the array contains specific elements. If you are not sure which method is suitable for you, it is recommended to choose the two most commonly used methods in_array() and array_search().

The above is the detailed content of How to determine whether an array contains a certain element in PHP (5 methods). For more information, please follow other related articles on the PHP Chinese website!

Statement:
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