This question has already been answered:

There is a simple array

$arr = array(2, 5, 5, 3 ,2, 1, 5, 7, 1); 

How to display only all unique non-duplicate values? Those. the output should be 2, 5, 3, 1, 7

The function array_unique ($ arr) cannot be used.

Reported as a duplicate by participants Alexey Shimansky , user194374, aleksandr barakin , Jean-Claude , katso 13 Dec '16 at 20:06 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • If it is вывести только все уникальные не повторяющиеся значения then the link to the duplicate is higher ↑ - Alexey Shimansky
  • Alexey looked duplicate on your link. But Pyramidhead below offered another good solution. - Beginner
  • In his decision, extra duplicates are simply removed ... and the не повторяющиеся уникальные элементы in your array are 3 and 7 ..... although, perhaps, you had in mind that option and not this ....... The twofold wording came out simply - Alexey Shimansky
  • Yes, I meant the extra doubles. - Beginner

2 answers 2

It is possible so:

 $arr = array(2, 5, 5, 3 ,2, 1, 5, 7, 1); $uniq_arr = array(); foreach ($arr AS $item) { if (!in_array($item, $uniq_arr)) { $uniq_arr[] = $item; echo $item . "\n"; } } 
  • perfect solution! Small code. Thank ! - Beginner
  • you can use array_count_values() or in addition to it array_filter() - teran

I do not know the features of the php language. But if you create a new array, var unique = []; and in the loop, check whether the element in src is contained, something like $arr.contains(_ell); if not contained, add to the unique array.

As for the syntax of the function that checks whether an element exists in the array, I’m not sure, because I don’t write to php.