在多维数组中查找重复值

时间:2021-01-23 21:37:45

From a function I am given a multidimensional array like 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 the 3 arrays within the main array. For example, if value 7 repeats in the 3 arrays, return 7.

我需要在主数组中的3个数组中找到重复值。例如,如果值3在3个数组中重复,则返回7。

4 个解决方案

#1


7  

<?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
?>

#2


2  

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 will sure help you.

希望这一定会对你有所帮助。

#3


1  

$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;

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

#4


1  

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

您将需要循环遍历第一个数组,并为其中的每个值查看它是否为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


7  

<?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
?>

#2


2  

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 will sure help you.

希望这一定会对你有所帮助。

#3


1  

$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;

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

#4


1  

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

您将需要循环遍历第一个数组,并为其中的每个值查看它是否为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).