Home  >  Article  >  Backend Development  >  How to determine whether a key-value pair exists in another array in php

How to determine whether a key-value pair exists in another array in php

PHPz
PHPzOriginal
2023-04-12 15:06:31925browse

In PHP programming, we usually use arrays to store and operate complex data. Sometimes, we need to find whether another array exists in an array. At this time, we can use some methods provided by PHP to achieve this.

1. Use the in_array() function to determine whether a value exists

in_array() is a function in PHP used to determine whether a value exists in an array. This function accepts two parameters: the value to be found and the array to be searched. Returns TRUE if the value is found, FALSE otherwise.

The sample code is as follows:

$array1 = array("apple", "banana", "orange");
$array2 = array("orange", "grape", "pear");

foreach ($array1 as $value) {
    if (in_array($value, $array2)) {
        echo "{$value} exists in array2\n";
    } else {
        echo "{$value} does not exist in array2\n";
    }
}

In the above sample code, we first define two arrays $array1 and $array2. Then in a foreach loop, iterate through the elements in array $array1 one by one, and use the in_array() function to determine whether the element exists in array $array2. If it exists, "{element} exists in array2" is output on the screen, otherwise "{element} does not exist in array2" is output.

2. Use the array_diff_assoc() function to compare key-value pairs

If we need to compare whether two arrays are completely equal, we need to compare not only the values, but also the key-value pairs. At this point, you can use the array_diff_assoc() function for comparison. This function returns key-value pairs that exist in the first array but do not exist in other arrays.

The sample code is as follows:

$array1 = array("a" => "apple", "b" => "banana", "c" => "orange");
$array2 = array("d" => "orange", "e" => "grape", "f" => "pear");

$diff = array_diff_assoc($array1, $array2);

print_r($diff);

In the above sample code, we define two associative arrays $array1 and $array2. Then use the array_diff_assoc() function to compare the key-value pairs of arrays $array1 and $array2 to obtain different key-value pairs $diff. Finally, use the print_r() function to output the contents of the $diff array.

3. Use the array_intersect_assoc() function to compare key-value pairs

If we need to compare two arrays to see if they have the same key-value pair, we can use the array_intersect_assoc() function. This function will return the same key-value pairs in both arrays.

The sample code is as follows:

$array1 = array("a" => "apple", "b" => "banana", "c" => "orange");
$array2 = array("d" => "orange", "e" => "grape", "f" => "pear", "a" => "orange");

$intersect = array_intersect_assoc($array1, $array2);

print_r($intersect);

In the above sample code, we define two associative arrays $array1 and $array2. Then use the array_intersect_assoc() function to compare the key-value pairs of arrays $array1 and $array2 to get the same key-value pair $intersect. Finally, use the print_r() function to output the contents of the $intersect array.

Summary

The above introduces the method of determining whether an array exists in another array in PHP: use the in_array() function to determine whether the value exists; use the array_diff_assoc() function to compare key-value pairs ;Use the array_intersect_assoc() function to compare key-value pairs. Through these methods, we can quickly find and compare various data in arrays in PHP programming.

The above is the detailed content of How to determine whether a key-value pair exists in another array 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