How can I get the repeated values inside a multidimensional array in PHP? I have found some functions but they have not worked for me. Is there a native function that does that?
<?php
$array = array(array(7,18), array(12,7), array(12, 7, 13));
$result = array();
$first = $array[0];
for($i=1; $i<count($array); $i++){
$result = array_intersect ($first, $array[$i]);
$first = $result;
}
print_r($result);//7
?>
function array_icount_values($arr,$lower=true) {
$arr2=array();
if(!is_array($arr['0'])){$arr=array($arr);}
foreach($arr as $k=> $v){
foreach($v as $v2){
if($lower==true) {$v2=strtolower($v2);}
if(!isset($arr2[$v2])){
$arr2[$v2]=1;
}else{
$arr2[$v2]++;
}
}
}
return $arr2;
}
$arr = array_icount_values($arry);
echo "<pre>";
print_r($arr);
exit;
I found this on Stackoverflow but neither one works for me.
If you have php5.6:
You can use this combination as long as the array has no more levels without the need for loops:
the function
array_merge
joins the elements of the array into a single array.array_count_values
returns the number of repetitions of each value within the array.With
array_diff($array,array(1))
we find the difference between our array of repetitions and an array with a single element with value 1. In essence we remove the keys with value 1.To finish, we get the keys with
array_keys
.Written on a single line:
You can use the php function
in_array()
It works in the following way:
What it does is check if a value is inside an array.
Here is a link to the documentation: http://php.net/manual/es/function.in-array.php