6

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.

Oldskool
  • 34,211
  • 7
  • 53
  • 66
a.4j4vv1
  • 123
  • 3
  • 11
  • Find what? You want remove or anything? – Bora Aug 26 '13 at 06:45
  • php offers a whole set of array examination and manipulation functions. I suggest you take a look at the documentation site, especially where those array functions are listed: http://www.php.net/manual/de/ref.array.php – arkascha Aug 26 '13 at 06:48
  • Please, clarify your goal. Now it's totally unclear – Alma Do Aug 26 '13 at 06:50

4 Answers4

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

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.

liyakat
  • 11,825
  • 2
  • 40
  • 46
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;

sudhakar
  • 582
  • 1
  • 3
  • 13
1

You will need to loop through the first array, and for each value in it, see if it 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).
Sutandiono
  • 1,748
  • 1
  • 12
  • 21