Home  >  Article  >  Backend Development  >  Determine whether multiple elements are in an array php

Determine whether multiple elements are in an array php

WBOY
WBOYOriginal
2023-05-11 10:23:06747browse

In PHP, it is very common to determine whether multiple elements are in an array. This article will introduce how to implement this function.

Method 1: Use the in_array function

The in_array function can determine whether an element is in an array. Its syntax is as follows:

bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )

It can be seen that this function has three parameters, the first parameter represents the target element, the second parameter represents which array to search in, and the third parameter represents Whether to enable strict mode. By default, strict mode is not enabled.

Therefore, we can use the in_array function to determine whether multiple elements are in the array through a loop. The sample code is as follows:

<?php
$needleArr = ['element1', 'element2', 'element3'];
$haystackArr = ['element1', 'element3', 'element4', 'element5'];

foreach ($needleArr as $needle) {
    if (in_array($needle, $haystackArr)) {
        echo "$needle is in haystack array 
";
    } else {
        echo "$needle is NOT in haystack array 
";
    }
}

The output result is:

element1 is in haystack array
element2 is NOT in haystack array
element3 is in haystack array

In the above code, we first define a target array $needleArr and an array to be searched $haystackArr, and then judge whether each element in $needleArr exists in $haystackArr through the foreach loop, and finally output the judgment result.

It should be noted that the in_array function does not enable strict mode by default, that is, convert elements into strings for comparison. If you need to enable strict mode, you can set the third parameter to true, as shown below:

in_array($needle, $haystackArr, true);

Method 2: Use the array_intersect function

The array_intersect function can compare multiple arrays and find them Common elements, the specific syntax is as follows:

array array_intersect (array $array1, array $array2 [, array $...])

This function can compare multiple arrays, among which the first The first parameter represents the first array, the second parameter represents the second array, and so on.

Therefore, we can also use the array_intersect function to pass in multiple arrays and find their intersection. If the intersection is not empty, it means that the specified element exists in the array. The sample code is as follows:

<?php
$needleArr = ['element1', 'element2', 'element3'];
$haystackArr = ['element1', 'element3', 'element4', 'element5'];

$intersectArr = array_intersect($needleArr, $haystackArr);
if (!empty($intersectArr)) {
    echo "There are some elements in the haystack array 
";
} else {
    echo "No elements in the haystack array 
";
}

The output result is:

There are some elements in the haystack array

In the above code, we first define a target array $needleArr and an array to be searched $haystackArr, and then pass them into the array_intersect function. If the returned intersection is not empty, it means that the specified element exists in the array.

It should be noted that the array_intersect function retains the keys in the original array, so the array it returns is not necessarily continuous, depending on whether the keys of the original array are continuous. If you need to preserve key-value relationships, you can use the array_intersect_assoc function.

Method 3: Use the array_diff function

array_diff function can compare multiple arrays and find their differences. The specific syntax is as follows:

array array_diff (array $array1, array $array2 [, array $... ] )

This function can compare multiple arrays, where the first parameter represents the first array, the second parameter represents the second array, and so on.

Therefore, we can also use the array_diff function to pass in multiple arrays and find their difference. If the difference is equal to the original array, it means that the specified element exists in the array. The sample code is as follows:

<?php
$needleArr = ['element1', 'element2', 'element3'];
$haystackArr = ['element1', 'element3', 'element4', 'element5'];

$diffArr = array_diff($needleArr, $haystackArr);
if ($diffArr !== $needleArr) {
    echo "There are some elements in the haystack array 
";
} else {
    echo "No elements in the haystack array 
";
}

The output result is:

There are some elements in the haystack array

In the above code, we first define a target array $needleArr and an array to be searched $haystackArr, and then pass them into the array_diff function. If the returned difference set is not equal to the original array, it means that the specified element exists in the array.

It should be noted that the array_diff function will return an array of elements that exist in the first array but do not exist in other arrays. So, if the first array itself does not contain the target element, the above method has no effect.

The above is the detailed content of Determine whether multiple elements are in an array 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