Find common values in multiple arrays with PHP
array_intersect()
$intersect = array_intersect($array1,$array2,$array3);
If you don't know how many arrays you have, then build up an array of arrays and user call_user_func_array()
$list = array();
$list[] = $array1;
$list[] = $array2;
$list[] = $array3;
$intersect = call_user_func_array('array_intersect',$list);
Finding common elements in multiple arrays php
array_intersect()
$intersect = array_intersect($array1,$array2,$array3);
If you don't know how many arrays you have, then build up an array of arrays and user call_user_func_array()
$list = array();
$list[] = $array1;
$list[] = $array2;
$list[] = $array3;
$intersect = call_user_func_array('array_intersect',$list);
Reference Here
How to get common values from two different arrays in PHP
Native PHP functions are faster than trying to build your own algorithm.
$result = array_intersect($array1, $array2);
Find Common most values in multidimensional array in PHP
You might use a foreach
and use the array values as the key and increment each times it matches.
At the end sort the keys using ksort and return the keys using array_keys :
$arrays = [
['000', '001', '002', '003'],
['000', '001', '002'],
['000', '001'],
['000', '001', '002', '003'],
['000', '001', '002'],
['000', '001'],
];
$result = [];
foreach ($arrays as $array) {
foreach ($array as $a) {
if (!array_key_exists($a, $result)) {
$result[$a] = 1;
continue;
}
$result[$a] += 1;
}
}
ksort($result);
print_r(array_keys($result));
Demo
Ouput
Array
(
[0] => 000
[1] => 001
[2] => 002
[3] => 003
)
Finding same values on multiple array data PHP
This works!
$arr1 = [1,2,3,4,5,9,14];
$arr2 = [1,2,10];
$arr3 = [1,2,5];
$arr4 = [1,2,3,5];
$duplicates = checkduplicate($arr1, $arr2, $arr3, $arr4);
print_r($duplicates);
function checkduplicate($arr1, $arr2, $arr3, $arr4)
{
$keys = [];
foreach($arr1 as $key)
{
if(in_array($key, $arr2) && in_array($key, $arr3) && in_array($key, $arr4))
{
$keys[] = $key;
}
}
return $keys;
}
This iterates over all the items in the first array, and check if they also contain in the others
You can also use array intersect, which takes multiple arrays
$duplicates = array_intersect($arr1, $arr2, $arr3, $arr4);
PHP – Find common string in value in multiple arrays and combine output
You can do it via iteration like this:
$bears = array(
array('label' => "Parent", 'ages' => "23 24 25 26 27 28 29 30 31 32 33"),
array('label' =>"Adult", 'ages' => "19 20 21 22 23 24 25 26 27 28 29"),
array('label' => "Teenager", 'ages' => "14 15 16 17 18")
);
$newArray = array();
foreach($bears as $bear)
{
if(strpos($bear['ages'], '23') !== false)
$newArray[] = $bear['label'];
}
echo("Results:\n");
var_dump($newArray);
Note you have to encapsulate "label" in quotes in your arrays as they are strings.
Find values that are present in all arrays (PHP)
Use array_intersect
$arrayOfArrays[] = [76,964,34,66775,3,88,99,69];
$arrayOfArrays[] = [69,55,65,657,436,6345,8654];
$arrayOfArrays[] = [54,543,496,3455,47556,69,234,436557];
$arrayOfArrays[] = [4564536,4543,777,3445,43768,2334,69,35,574];
$intersect = array_intersect(
$arrayOfArrays[0],$arrayOfArrays[1],$arrayOfArrays[2],$arrayOfArrays[3]
);
Or recursively like the answer by @Mr J
$arrayOfArrays[] = [76,964,34,66775,3,88,99,69];
$arrayOfArrays[] = [69,55,65,657,436,6345,8654];
$arrayOfArrays[] = [54,543,496,3455,47556,69,234,436557];
$arrayOfArrays[] = [4564536,4543,777,3445,43768,2334,69,35,574];
$intersect = call_user_func_array('array_intersect', $arrayOfArrays);
How to find common elements in two arrays?
There is an array_intersect
function that does pretty much what you need. If you want to compare the elements using regular expressions, try preg_grep
, but you will need to prepare the pattern first.
Related Topics
Composer Installing: the JSON Extension Is Missing
How to Get Output of Proc_Open()
Package 'Php5-Gd' Has No Installation Candidate
How to Zip Folder Without Full Path
PHP Curl Localhost Is Slow When Making Concurrent Requests
Naming Convention Issues When Using Codeigniter in Windows and Linux
How to Tell If a File Is Text Using PHP
PHP Passing C Struct Data Throught Socket Programming
Why We Need Curlopt_Ssl_Verifypeer in Windows
PHP Move_Uploaded_File() Fails Without Reason