Home  >  Article  >  Backend Development  >  How to determine if a string already exists in an array in php

How to determine if a string already exists in an array in php

PHPz
PHPzOriginal
2023-04-23 09:17:13575browse

Determining whether a string already exists in an array is a common problem in PHP. In actual development, you may encounter the need to determine whether a string has been stored in an array to avoid data of repetition.

This article will introduce how to determine whether a string already exists in an array in PHP.

  1. Use the in_array() function

PHP provides an in_array() function, which can be used to determine whether a specified value already exists in an array.

The function prototype is as follows:

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

Among them, the $needle parameter is the value to be found, the $haystack parameter is the array to be searched, and the $strict parameter indicates whether to use strict mode. In strict mode , in addition to matching types, values ​​must also be matched.

The following is an example of using the in_array() function:

<?php

$arr = array("apple", "banana", "orange");

if (in_array("apple", $arr)) {
    echo "字符串存在于数组中";
} else {
    echo "字符串不存在于数组中";
}

?>

In the above example, $arr is an array, which contains three strings "apple", "banana", "orange". We use the in_array() function to determine whether the string "apple" is in the array $arr. If it exists, it will output "the string exists in the array", otherwise it will output "the string does not exist in the array".

  1. Use the array_search() function

PHP also provides an array_search() function to search for a given value in the array and return the corresponding key name.

The function prototype is as follows:

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

Among them, the $needle parameter is the value to be found, the $haystack parameter is the array to be searched, and the $strict parameter indicates whether to use strict mode. In strict mode , in addition to matching types, values ​​must also be matched.

The following is an example of using the array_search() function:

<?php

$arr = array("apple", "banana", "orange");

if (array_search("apple", $arr) !== false) {
    echo "字符串存在于数组中";
} else {
    echo "字符串不存在于数组中";
}

?>

In the above example, $arr is an array, which contains three strings "apple", "banana", "orange". We use the array_search() function to determine whether the string "apple" is in the array $arr. If it exists, it will output "The string exists in the array", otherwise it will output "The string does not exist in the array". It should be noted that since the array_search() function may return 0, we need to use the !== operator for strict comparison.

  1. Use in_array() function and array_map() function

In addition to in_array() function and array_search() function, you can also use in_array() function and array_map( ) function to determine whether a string already exists in the array.

<?php

$arr = array("apple", "banana", "orange");

function exists_in_array($str, $arr) {
    return in_array($str, $arr);
}

if (in_array("apple", array_map("exists_in_array", "apple", $arr))) {
    echo "字符串存在于数组中";
} else {
    echo "字符串不存在于数组中";
}

?>

In the above example, we defined a function named exists_in_array(). The function of the function is to determine whether a string already exists in the given array. We then use the array_map() function to pass each element in the array $arr as the second argument to the exists_in_array() function, and pass the character "apple" as the first argument to the exists_in_array() function. Finally, we pass the returned result to the in_array() function to determine whether the string "apple" is in the array $arr. If it exists, it outputs "The string exists in the array", otherwise it outputs "The string does not exist in the array." middle".

Conclusion

This article introduces three methods in PHP on how to determine whether a string already exists in an array, namely using the in_array() function, array_search() function and in_array() function. and a combination of array_map() functions. Using these methods can effectively avoid data duplication and improve program performance.

The above is the detailed content of How to determine if a string already exists in an array in php. 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