Home  >  Article  >  Backend Development  >  php query array whether array exists

php query array whether array exists

WBOY
WBOYOriginal
2023-05-06 12:42:09531browse

In PHP development, we often encounter situations where we need to query whether an array exists in another array. This situation usually occurs in scenarios where it is necessary to determine whether the data submitted by the user is legal and whether the relationship between the two tables exists. In response to this situation, this article will introduce several methods in PHP to query whether an array exists.

Method 1: Use the array_diff function

The array_diff function can be used to calculate the difference set of the array. If the two arrays have no intersection, the original array is returned. Otherwise, the elements outside the intersection are returned, that is, the difference set. . Therefore, we can use the array_diff function to determine whether there is an intersection between two arrays. If there is an intersection, it means that the elements between the arrays are the same. Otherwise, they do not exist.

Sample code:

if (count(array_diff($arr1, $arr2)) == 0) {
    echo "数组arr1存在于数组arr2中";
} else {
    echo "数组arr1不存在于数组arr2中";
}

In the above code, we compare the difference set between the arrays $arr1 and $arr2. If the difference set is empty, it means that the array $arr1 completely exists in the array $arr2.

Method 2: Use array_intersect function

The array_intersect function can be used to calculate the intersection of arrays. If there is an intersection between two arrays, the elements of the intersection part are returned. If there is no intersection, an empty array is returned. Therefore, we can use the array_intersect function to determine whether there is an intersection between two arrays. If there is an intersection, it means that the same elements exist between the arrays. Otherwise, they do not exist.

Sample code:

if (count(array_intersect($arr1, $arr2)) > 0) {
    echo "数组arr1存在于数组arr2中";
} else {
    echo "数组arr1不存在于数组arr2中";
}

In the above code, we compare the intersection between the arrays $arr1 and $arr2. If the intersection is not empty, it means that there is at least one element in the array $arr1. in array $arr2.

Method 3: Use in_array function

The in_array function can be used to determine whether an element exists in an array. Therefore, we can loop through using the in_array function to determine whether each element in array $arr1 exists in array $arr2. If all elements in array $arr1 exist in array $arr2, it means that array $arr1 completely exists in array $arr2. in array $arr2, otherwise it does not exist.

Sample code:

$flag = true;
foreach ($arr1 as $value) {
    if (!in_array($value, $arr2)) {
        $flag = false;
        break;
    }
}
if ($flag) {
    echo "数组arr1存在于数组arr2中";
} else {
    echo "数组arr1不存在于数组arr2中";
}

In the above code, we loop to determine whether each element in array $arr1 exists in array $arr2, if all elements in array $arr1 exist In the array $arr2, define a $flag variable and assign it a value of true, otherwise modify the value of $flag to false. Finally, determine whether array $arr1 exists in array $arr2 based on the value of $flag.

The above are several ways to query whether an array exists in PHP. Developers can choose different methods according to actual business scenarios.

The above is the detailed content of php query array whether array exists. 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