1

i have array which has multiple duplicate values. now i want to get those duplicate values.

           $roQ = Array
           (
    [0] => 2123 
    [1] => 2094 
    [2] => 2105 
    [3] => 2160 
    [4] => 2143 
    [5] => 2148 
    [6] => 2154 
    [7] => 2155
    [8] => 2145
    [9] => 2123 
    [10] => 2149
     [11] => 2143 
     [12] => 2145

       )

i tried following code which is not working. it returns incorrect result. how to get the all duplicate values in new array.

   $c = array_count_values($roQ); 
       $val = array_search(max($c), $c);
      $azq[] = $val;
Jack Torris
  • 814
  • 5
  • 23
  • 38

3 Answers3

0

Try with -

$arr = array(1, 4, 6, 1, 8, 9, 4, 6);

$unique = array_unique($arr);

$duplicates = array_diff_assoc($arr, $unique);

var_dump($duplicates);
Sougata Bose
  • 31,517
  • 8
  • 49
  • 87
0
$roQ = Array
           (
    [0] => 2123 
    [1] => 2094 
    [2] => 2105 
    [3] => 2160 
    [4] => 2143 
    [5] => 2148 
    [6] => 2154 
    [7] => 2155
    [8] => 2145
    [9] => 2123 
    [10] => 2149
     [11] => 2143 
     [12] => 2145

       )

for($i=0; $i<= count($roQ), $i++){

  if(!in_array($roQ[$i],$arr){

     $unique_value[] = $roQ[$i]; 

  }else {

     $dublicate_array[]=$roQ[$i]

  }

   array_push($arr, $req[$i]);

}
SuReSh
  • 1,503
  • 1
  • 22
  • 47
0

Try this code it displays the duplicate elements in an array.

$array = array(
            0 => 2123,
            1 => 2094,
            2 => 2105,
            3 => 2160,
            4 => 2143,
            5 => 2148,
            6 => 2154,
            7 => 2155,
            8 => 2145,
            9 => 2123,
            10 => 2149,
            11 => 2143,
            12 => 2145

        );
        $arrayNew = array_count_values($array);
        $finalArr = array ();
        $i=0;
        foreach ($arrayNew as $key => $Array_new){
            if($Array_new>1){

                 $finalArr[$i]  = $key;
                 $i++;
            }
       }
        echo "<pre>";
        print_r($finalArr);
        exit;
    }
Harigovind R
  • 816
  • 8
  • 17