Home >Backend Development >PHP Problem >How to search an array using keywords in php

How to search an array using keywords in php

PHPz
PHPzOriginal
2023-04-27 09:03:15755browse

In PHP, you can use keywords to search an array to find out whether a specific value exists in the array. This process can be achieved in a variety of ways. This article will introduce two implementation methods.

Method 1: Use the in_array() function

PHP provides an in_array() function, which is used to determine whether a value exists in the array. When using this function, you need to pass in two parameters. The first parameter is the value you want to find, and the second parameter is the array you want to search. This function returns a Boolean value, which returns true if the value to be found exists in the array; otherwise it returns false.

The following code is a simple example, used to find whether a specified value exists in an array:

$arr = array(1, 3, 5, 7, 9);

if (in_array(5, $arr)) {
    echo '5存在于数组中';
} else {
    echo '5不存在于数组中';
}

In the above example, the parameters passed into the in_array() function are 5 and $arr respectively. , the in_array() function will find whether there is an element with a value of 5 in the array $arr. Because 5 does exist in the array, the program will output "5 exists in the array".

Of course, we can simply modify the function so that it returns the subscripts of all elements that meet the search criteria. The specific operation is as follows:

$arr = array(1, 3, 5, 7, 9, 2, 4, 6, 8);

$index = array_keys($arr, 5);

if (count($index) > 0) {
    foreach ($index as $value) {
        echo '5存在于数组的第' . $value . '个位置';
    }
} else {
    echo '5不存在于数组中';
}

In the above example, we first use the array_keys() function to obtain the subscript array of all elements that meet the conditions. If the number of elements in the array is greater than 0, then output each element that meets the conditions. The element subscript of the condition; otherwise output "5 does not exist in the array".

Method 2: Use foreach loop to traverse the array

In addition to using the in_array() function, we can also use foreach loop to traverse the array to find whether there are elements that meet the conditions. When using this method, you need to loop through the array first, take out one element in the array each time, and determine whether it meets the search conditions. If an element that meets the criteria is found, the subscript or value of the element is output, otherwise "no element that meets the criteria is found" is output.

The following code is a simple example for finding whether a specified value exists in an array:

$arr = array(1, 3, 5, 7, 9);
$isFind = false;

foreach ($arr as $key => $value) {
    if ($value == 5) {
        echo '5存在于数组中,下标为:' . $key;
        $isFind = true;
        break;
    }
}

if (!$isFind) {
    echo '没有找到符合条件的元素';
}

In the above example, we traverse each element in the array and assign the array subscript Give variable $key and assign array elements to variable $value. If an element that meets the conditions is found, the subscript of the element and the value of the variable $key (that is, the subscript of the array element) are output; otherwise, "No element that meets the conditions is found" is output.

Conclusion:

Both of the above two methods use concise code to implement keyword search in arrays. In order to utilize these methods in actual work, they need to be modified and expanded according to the actual situation to achieve better results.

The above is the detailed content of How to search an array using keywords 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