Find duplicate value in multidimensional array

From the function, I was given a multidimensional array similar to this:

array( [0] => array( [0] => 7, [1] => 18 ), [1] => array( [0] => 12, [1] => 7 ), [2] => array( [0] => 12, [1] => 7, [2] => 13 ) ) 

I need to find duplicate values ​​in 3 arrays in the main array. For example, if the value 7 is repeated in 3 arrays, return 7.

+6
source share
4 answers
 <?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 ?> 
+7
source

use my custom function

 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; 

Ouput

 Array ( [7] => 3 [18] => 1 [12] => 2 [13] => 1 ) 

Hope this helps you.

+2
source
 $input = array("a" => "green", "red", "b" => "green", "blue", "red"); $result = array_unique($input); print_r($result); 

use this code

 $array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red'); 

$ key = array_search ('green', $ array); // $ key = 2;

+1
source

You will need to skip the first array, and for each value in it, see if there is in_array () .

 $findme = array(); foreach ($array[0] as $key => $value) { if (in_array ($value, $array[1]) && in_array ($value, $array[2])) { $findme[] = $value; } } // $findme will be an array containing all values that are present in all three arrays (if any). 
+1
source

Source: https://habr.com/ru/post/952483/


All Articles