Given the following arrays:
$array[0]['tid'] = 'valueX';
$array[1]['tid'] = 'valueY';
$array2[0]['tid'] = 'valueZ';
$array2[1]['tid'] = 'valueY';
My goal is to check if any of the values in $array are in $array2
Below is what I came up with but I’m wondering if there is a easier / better solution?
Maybe something that gets only the values of an array or removes the 'tid' key.
foreach($array as $arr) {
$arr1[] = $arr['tid'];
}
$flag = 0;
foreach($array2 as $arr) {
if( in_array( $arr['tid'], $arr1 ) ) {
$flag++;
}
}
echo $flag; // number of duplicates
One way you could approach this:
What this does is pull out the
tidvalue from each array element on both arrays, then intersecting the results (to find values present in both arrays). It might make sense to tweak it a bit or keep the interim results around depending on what exactly you intend to do.See it in action.