0

I Have array As Follow

Array
(
    [0] => myProduct_1
    [1] => myProduct_2
    [2] => myProduct_3
    [3] => myProduct_2
    [4] => myProduct_1
    [5] => myProduct_1
    [6] => myProduct_1
    [7] => myProduct_1
)

Now i need only values which is more than one time in array. So how can i get ?

NOTE: here name of array value is not same every-time it changes as blah,blah.

Mr. Hola
  • 39
  • 9

1 Answers1

0

here is the simple solution, I have get the count of each duplicate and then remove the ones whose having a single count

<?php
// just replace your array here
$array = [1, 2, 3, 1, 2];
$output = [];
foreach ($array as $key => $value) {
    $output[$value] += 1;
}

$array = [];
foreach ($output as $key => $value) {
    if($value > 1)
    $array[] = $key;
}

echo '<pre>';
print_r($array);
?>
// output
Array
(
 [0] => 1
 [1] => 2
)
pravindot17
  • 1,199
  • 1
  • 15
  • 32