Search for repeated values in multidimensional array (PHP)

2

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 in Stackoverflow but it does not work for me either.

    
asked by RuralGalaxy 09.08.2016 в 09:12
source

3 answers

3

If you have php5.6:

<?php
// Array multidimensional
$a = [
  [1,2,3,4,5,6, 33], 
  [1,2,5,6,7,9], 
  [3,4,1,2,8,5, 
    [1,2,3,9, 
      [8, 
        [34]
      ]
    ] , 
    33, 34
  ]
];

// Primero hacemos la matriz de una sola dimensión
$b = [];
array_walk_recursive($a, function($a) use (&$b) {
  $b[] = $a;
});

/* En caso de ser un array de 2 dimensiones podemos usar
$b = call_user_func_array('array_merge', $a);
ó
$b = array_merge(...$a); <-- en el caso de utilizar PHP > 5.6

*/ 

// array_count_values: cuenta cada valor retornando un array (valor => cantidad)
// luego filtramos los que tengan un valor > 1
// luego recuperamos las keys (que son los valores originales
$repetidos = array_keys(
               array_filter(
                 array_count_values($b), function($v, $k) {
                   return $v > 1;
               }, ARRAY_FILTER_USE_BOTH)
             ); // es necesario PHP 5.6 para utilizar tanto keys como values en array_filter

var_dump($repetidos);

// OUTPUT:
array(6) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
  [3]=>
  int(4)
  [4]=>
  int(5)
  [5]=>
  int(6)
}
    
answered by 09.08.2016 / 12:32
source
2

You can use this combination as long as the array does not have more levels without the need for loops:

$result = call_user_func_array('array_merge',$array);
$result = array_count_values ($result);
$result = array_diff($result,array(1));
$result = array_keys($result);

the function array_merge joins the elements of the array in 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 eliminate the keys with value 1.

To finish, we get the keys with array_keys .

Written in a single line:

$result = array_keys(array_diff(array_count_values (call_user_func_array('array_merge',$array)),array(1)));
    
answered by 09.08.2016 в 11:40
0

You can use the php in_array()

function

It works as follows:

<?php
$os = array("Mac", "NT", "Irix", "Linux");
if (in_array("Irix", $os)) {
    echo "Existe Irix";
}
if (in_array("mac", $os)) {
    echo "Existe mac";
}
?>

What it does is check if a value is inside an array.

I leave a link to the documentation: link

    
answered by 09.08.2016 в 09:14