In PHP, the array_intersect() function is used to find the values that appear together in multiple arrays. It returns a new array containing the same values present in all arrays. Today we will explore how to use this function and further count the number of times these common values appear in the original array.
The array_intersect() function accepts multiple arrays as parameters and returns an array containing the intersection parts of multiple arrays, that is, there are values that appear in all arrays. It should be noted that it simply compares values, ignoring the key names of elements in the array.
array_intersect(array $array1, array $array2, array ...$arrays): array
$array1, $array2, ...$arrays : One or more arrays to compare.
Return Value: A new array containing the values found in all input arrays.
Below we use a simple example to show how to use array_intersect() to find the values that appear together in multiple arrays and count their occurrences in the original array.
<?php
// Define three arrays
$array1 = array(1, 2, 3, 4, 5, 6);
$array2 = array(4, 5, 6, 7, 8);
$array3 = array(3, 4, 5, 9);
// Find the common values in these three arrays
$commonValues = array_intersect($array1, $array2, $array3);
// Output a common value
echo "The value that appears together is:\n";
print_r($commonValues);
// Statistics the number of occurrences of these common values in the original array
echo "\nStatistics the number of occurrences of common values:\n";
foreach ($commonValues as $value) {
$count1 = count(array_keys($array1, $value));
$count2 = count(array_keys($array2, $value));
$count3 = count(array_keys($array3, $value));
echo "value $value Appears in the array1Number of times:$count1\n";
echo "value $value Appears in the array2Number of times:$count2\n";
echo "value $value Appears in the array3Number of times:$count3\n";
}
?>
We define three arrays $array1 , $array2 , $array3 .
Use the array_intersect() function to find the common values in these three arrays, and the result is saved in $commonValues .
For each common occurrence value, we use the array_keys() function to find out its position in each original array, and then calculate its occurrence number through the count() function.
Finally, we output the number of occurrences of each common value in three arrays.
Sometimes, we may need to process some array containing URL addresses. Suppose we have multiple arrays containing different URLs, we can find the common URL through the array_intersect() function and then count the number of times each URL appears. To demonstrate this function, the following is the modified code, and the domain name in the URL has been replaced with m66.net :
<?php
// Define three arrays,It contains URL
$array1 = array("https://m66.net/page1", "https://m66.net/page2", "https://m66.net/page3");
$array2 = array("https://m66.net/page3", "https://m66.net/page4", "https://m66.net/page5");
$array3 = array("https://m66.net/page2", "https://m66.net/page3", "https://m66.net/page6");
// Find out what is common in these three arrays URL
$commonUrls = array_intersect($array1, $array2, $array3);
// Output common URL
echo "Co-appearing URL yes:\n";
print_r($commonUrls);
// Statistics of these common URL Number of occurrences
echo "\nStatistical common URL Number of occurrences:\n";
foreach ($commonUrls as $url) {
$count1 = count(array_keys($array1, $url));
$count2 = count(array_keys($array2, $url));
$count3 = count(array_keys($array3, $url));
echo "URL $url Appears in the array1Number of times:$count1\n";
echo "URL $url Appears in the array2Number of times:$count2\n";
echo "URL $url Appears in the array3Number of times:$count3\n";
}
?>
The arrays $array1 , $array2 , $array3 contain the URL address with the m66.net domain name.
Use the array_intersect() function to find the URL address common in the three arrays.
For each common URL, use the array_keys() and count() functions to count it's occurrences in each array.