Home >Backend Development >PHP Problem >How to determine if an array is contained in another array in php
In PHP development, we often need to determine whether an array is contained in another array. This situation is very common in both data processing and logic processing. This article will introduce several methods so that developers can choose the most appropriate way to determine whether an array is contained in another array based on the actual situation.
Method 1: Use the array_diff function
The array_diff function is a built-in function in PHP that compares two arrays and returns the difference. Because only the difference set is returned, we can determine whether one array is contained in another array by determining whether the difference set is an empty array. The following is a sample code:
$array1 = [1, 2, 3, 4, 5]; $array2 = [1, 2, 3]; // 比较两个数组并返回差集 $diff = array_diff($array2, $array1); if (empty($diff)) { echo "array2 包含于 array1 中"; } else { echo "array2 不包含于 array1 中"; }
In the above code, two arrays $array1 and $array2 are first defined. $array2 is a subset contained in $array1. Then use the array_diff function to calculate the difference set, and use the empty function to determine whether the difference set is an empty array, and finally output the result.
Method 2: Use the array_intersect function
Similar to the array_diff function, the array_intersect function is also a built-in function in PHP, used to compare two arrays and return the intersection. The difference is that array_intersect returns the intersection, which can determine whether an array is contained by another array. The following is a sample code:
$array1 = [1, 2, 3, 4, 5]; $array2 = [1, 2, 3]; // 比较两个数组并返回交集 $intersect = array_intersect($array2, $array1); if ($intersect == $array2) { echo "array2 包含于 array1 中"; } else { echo "array2 不包含于 array1 中"; }
In the above code, two arrays $array1 and $array2 are first defined. $array2 is a subset contained in $array1. Then use the array_intersect function to calculate the intersection, and by judging whether the intersection is equal to $array2, the final result is output.
Method 3: Use in_array function
The in_array function is a built-in function in PHP, used to check whether a value exists in an array. We can check whether each element in the $array2 array exists in $array1. If every element is present in $array1, then $array2 can be said to be a subset of $array1. The following is a sample code:
$array1 = [1, 2, 3, 4, 5]; $array2 = [1, 2, 3]; $flag = true; // 检查 $array2 中的每个元素是否存在于 $array1 中 foreach ($array2 as $value) { if (!in_array($value, $array1)) { $flag = false; break; } } if ($flag) { echo "array2 包含于 array1 中"; } else { echo "array2 不包含于 array1 中"; }
In the above code, two arrays $array1 and $array2 are first defined. $array2 is a subset contained in $array1. Then iterate through each element in $array2 via foreach and check if the element exists in $array1 using the in_array function. If each element exists in $array1, $flag is assigned true, otherwise it is assigned false. Finally, the result is output based on the value of $flag.
Method 4: Use the array_intersect_key function
The array_intersect_key function is a built-in function in PHP that compares the keys of two arrays and returns the intersection. We can use the array_intersect_key function to determine whether the keys of one array all exist in another array. If present, the array is considered to be contained within another array. The following is a sample code:
$array1 = ['a'=>1, 'b'=>2, 'c'=>3, 'd'=>4, 'e'=>5]; $array2 = ['a'=>1, 'b'=>2, 'c'=>3]; // 比较两个数组的键并返回交集 $intersect = array_intersect_key($array2, $array1); if ($intersect == $array2) { echo "array2 包含于 array1 中"; } else { echo "array2 不包含于 array1 中"; }
In the above code, two associative arrays $array1 and $array2 are first defined. $array2 is a subset contained in $array1. Then use the array_intersect_key function to calculate the intersection of keys, and determine whether the intersection is equal to $array2, and finally output the result.
Summary
This article introduces 4 methods for determining whether an array is contained in another array. Among them, using the array_diff function and array_intersect function are the more common and simple methods. Using the in_array function and array_intersect_key function is more precise and allows you to flexibly determine the contents of the array.
If the array that needs to be judged is large, it is recommended to use the latter two methods, because the array_diff and array_intersect functions may have an impact on the performance of large arrays. At the same time, developers can also use these methods comprehensively based on actual conditions to achieve the best results.
The above is the detailed content of How to determine if an array is contained in another array in php. For more information, please follow other related articles on the PHP Chinese website!