In PHP, you can check if every element of the first array exists in the second array, in the following ways:
Using array_diff()
To check if an array contains all the values from another array, you can use the array_diff()
function to find the difference between the two arrays. If the result is an empty array, then it means one of the following two:
- The first array is empty and the second array is not empty, or;
- The first array contains all the values from the second array.
Therefore, you can create an explicit check for the first case to return false
early in the code, and if it passes you can return true
/false
depending on the difference of the two arrays.
For example:
function hasAllElems(array $arr1, array $arr2): bool { if (empty($arr1) && ! empty($arr2)) { return false; } return ! array_diff($arr1, $arr2); } var_dump(hasAllElems([2, 4], [1, 2, 3, 4, 5])); // true var_dump(hasAllElems([1, 2, 3, 4, 5], [2, 4])); // false var_dump(hasAllElems([], [])); // true var_dump(hasAllElems([], [1, 2, 3, 4, 5])); // false var_dump(hasAllElems([2, 4], [])); // false
Using array_intersect()
You can use the array_intersect()
function to find the intersection (or common elements) between the two arrays. If the number of elements in the resulting array are the same amount as in the first array, then it means that the first array contains all the values from the second array.
For example:
function hasAllElems(array $arr1, array $arr2) { if (empty($arr1) && ! empty($arr2)) { return false; } $result = array_intersect($arr1, $arr2); return count($result) === count($arr1); } var_dump(hasAllElems([2, 4], [1, 2, 3, 4, 5])); // true var_dump(hasAllElems([1, 2, 3, 4, 5], [2, 4])); // false var_dump(hasAllElems([], [])); // true var_dump(hasAllElems([], [1, 2, 3, 4, 5])); // false var_dump(hasAllElems([2, 4], [])); // false
Please note the edge case when the first array is empty and the second array is not empty. In this case, array_intersect()
would return an empty array, resulting in a false positive. Therefore, an explicit check is added for this to return false
early in the code.
Using a Loop
You can simply iterate over the first array and check in each iteration if the current array element exists in the second array. If not, then return false
, otherwise continue till the end of the array.
For example:
function hasAllElems(array $arr1, array $arr2) { if (empty($arr1) && ! empty($arr2)) { return false; } $containsAll = true; foreach ($arr1 as $value) { if (! in_array($value, $arr2)) { $containsAll = false; break; } } return $containsAll; } var_dump(hasAllElems([2, 4], [1, 2, 3, 4, 5])); // true var_dump(hasAllElems([1, 2, 3, 4, 5], [2, 4])); // false var_dump(hasAllElems([], [])); // true var_dump(hasAllElems([], [1, 2, 3, 4, 5])); // false var_dump(hasAllElems([2, 4], [])); // false
Please note the edge case when the first array is empty and the second array is not empty. For this case, an explicit check is added to return false
early in the code, otherwise true
would have been returned as the loop is never entered, resulting in a false positive.
Hope you found this post useful. It was published . Please show your love and support by sharing this post.