0

i want to print array duplicate, example :

$input = ['a', 'a', 'b'];
$output = array_unique($input);
echo $output;

i run code in php online ,but output just like this..

Array

how to echo "A" because "A" is duplicate alphabet from array above ?

Bian
  • 95
  • 2
  • 10
  • 1
    There are several ways. But I'm not sure what you want. Basically you want to display all the dups? What if it is [a,a,b,b,c]? What will you display? – Forbs Jan 26 '19 at 05:38
  • 1
    Check this _—>https://stackoverflow.com/questions/5995095/show-only-duplicate-elements-from-an-array – Swati Jan 26 '19 at 05:40
  • Possible duplicate of [Show only duplicate elements from an array](https://stackoverflow.com/questions/5995095/show-only-duplicate-elements-from-an-array) – Nick Jan 26 '19 at 07:05

3 Answers3

0

Your code works, you just cannot print an entire array with 'echo' statements. As written, you can print individual elements of $output (for example echo $output[0]; or echo $output[1];). If you want to print the whole array, use print_r($output); or var_dump($output);

Russ J
  • 828
  • 5
  • 12
  • 24
0

Array_unique will not give you that information, it will only delete the duplicates.
If you count the values with array_count_values then use array_diff to get what is not 1, then the code will return the duplicated items.
But it will return them in keys, so use array_keys to get them as values.

$input = ['a', 'a', 'b'];
$count = array_count_values($input);
$duplicated = array_keys(array_diff($count, [1]));
var_export($duplicated);

https://3v4l.org/2HS8e

You can also echo the duplicates with:

echo implode(", ", $duplicated);
Andreas
  • 23,610
  • 6
  • 30
  • 62
0

Just use array_diff or array_diff_assoc for compute two array. (before and after doing remove duplicate by using array_unique)

Example :

$array = ['a','b','c'];
$array2 = $array;

echo "<pre>";
var_dump($array);
echo "</pre>";

$array = array_unique($array);
$diff = array_diff_assoc($array2,$array);

echo '<hr>Diff : ';
echo "<pre>";
var_dump($diff);
echo "</pre>";
exit();

See the result here.

enter image description here

Zuko
  • 358
  • 1
  • 7
  • 1
    Avoid posting code (and output) as images, see https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-on-so-when-asking-a-question – Progman Jan 26 '19 at 12:28