Home  >  Article  >  Backend Development  >  How to determine whether there is a string element in an array in php

How to determine whether there is a string element in an array in php

PHPz
PHPzOriginal
2023-04-26 10:33:03586browse

With the rapid development of Internet technology, programming languages ​​​​in the field of Web development are also constantly evolving and developing. PHP, as an open source programming language, is widely used in the development of web applications. In PHP, array is one of the commonly used data types. During the development process, PHP is often used to determine whether a string exists in an array. This article will introduce several methods to achieve this function.

Method 1: Use the in_array() function

The in_array() function is a function in PHP used to determine whether a value exists in an array. Its use is relatively simple. We can pass the string to be judged as the first parameter to the in_array() function, and the array to be searched as the second parameter to the function. If the return value is TRUE, it means that the string does exist in the array, otherwise it means that it does not exist. The following is an example of using the in_array() function to determine whether a string exists in an array:

<?php
//定义一个数组
$fruit = array(&#39;apple&#39;, &#39;banana&#39;, &#39;orange&#39;);

//判断字符串是否在数组中
if (in_array(&#39;apple&#39;, $fruit)) {
    echo &#39;存在&#39;;
} else {
    echo &#39;不存在&#39;;
}
?>

The output of running the above code is "exists".
Method 2: Use the array_search() function

The array_search() function is a function in PHP used to find the key of a value in an array. It can also be used to determine whether a value exists. in the array. If found, the key will be returned as the return value, otherwise FALSE will be returned. The following is an example of using the array_search() function to determine whether a string exists in an array:

<?php
//定义一个数组
$fruit = array(&#39;apple&#39;, &#39;banana&#39;, &#39;orange&#39;);

//查找数组中是否存在字符串
$search = array_search(&#39;apple&#39;, $fruit);

if ($search !== FALSE) {
    echo &#39;存在&#39;;
} else {
    echo &#39;不存在&#39;;
}
?>

The output of running the above code is "exists".
Method 3: Use in_array() function and array_keys() function

The in_array() function and array_keys() function are both PHP array functions, which can be used to determine whether a value exists in the array. The in_array() function is used to determine whether a value exists, while the array_keys() function outputs all keys of a value in the array. The following is an example of using the in_array() function and array_keys() function to determine whether a string exists in the array:

<?php
//定义一个数组
$fruit = array(&#39;apple&#39;, &#39;banana&#39;, &#39;orange&#39;);

//查找数组中是否存在字符串,并输出其键
if (in_array(&#39;apple&#39;, $fruit)) {
    $keys = array_keys($fruit, &#39;apple&#39;);
    echo &#39;存在,键为:&#39; . implode(&#39;,&#39;, $keys);
} else {
    echo &#39;不存在&#39;;
}
?>

The output result of running the above code is "Exists, key: 0".

The above are several ways to determine whether a string exists in a PHP array. In actual development, we choose different methods according to actual conditions to achieve better performance and effects.

The above is the detailed content of How to determine whether there is a string element 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