Kani R
Kani R

Reputation: 189

Separate flat array values into an array of unique values and an array of duplicate values

I want to find unique values. I don't want to array format, I want only string.

<?php
$array = array("kani","yuvi","raja","kani","mahi","yuvi") ;
$unique_array = array(); // unique array
$duplicate_array = array(); // duplicate array
foreach ($array as $key => $value) {
  if (!in_array($value,$unique_array)) {
       $unique_array[$key] = $value;
  } else {
     $duplicate_array[$key] = $value;
  }
}
echo "unique values are:-<br/>";
echo "<pre/>";print_r($unique_array);

echo "duplicate values are:-<br/>";
echo "<pre/>";print_r($duplicate_array);

Upvotes: -1

Views: 9156

Answers (4)

user26887009
user26887009

Reputation:

Here is my solution :

function getUniqueOrDuplicateValuesInArray(array $myArray, bool $wantUnique = true): array
{
    return array_keys(array_filter(array_count_values($myArray), function ($count) use($wantUnique) {
        return $wantUnique ? $count === 1 : $count > 1;
    }));
}

With array_count_values(), I'm counting the number of occurences of the values in my array.

With array_filter(), it's here that I'll choose if I want unique values or duplicates using $wantUnique parameter.

Since array_count_values() put the values in keys, I'm then using array_keys() to get the values.

Upvotes: 0

sumityadavbadli
sumityadavbadli

Reputation: 251

<?php
  $arr1 = [1,1,2,3,4,5,6,3,1,3,5,3,20];    
  print_r(arr_unique($arr1)); // will print unique values
  echo "<br>";
  arr_count_val($arr1); // will print array with duplicate values

  function arr_unique($arr) {
    sort($arr);
    $curr = $arr[0];
    $uni_arr[] = $arr[0];
    for($i=0; $i<count($arr);$i++){
       if($curr != $arr[$i]) {
         $uni_arr[] = $arr[$i];
         $curr = $arr[$i];
       }
    }
    return $uni_arr;
  }

  function arr_count_val($arr) {
    $uni_array = arr_unique($arr1);
    $count = 0;

    foreach($uni_array as $n1) {
      foreach($arr as $n1) {
        if($n1 == $n2) {
          $count++;
        }
      }
    echo "$n1"." => "."$count"."<br>";
    $count=0;
    }
  }
?>

Upvotes: 0

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72299

You can use array_unique() in single line like below:-

<?php

$unique_array = array_unique($array); // get unique value from initial array

echo "<pre/>";print_r($unique_array); // print unique values array
?>

Output:- https://eval.in/601260

Reference:- http://php.net/manual/en/function.array-unique.php

If you want in string format:-

echo implode(',',$final_array);

Output:-https://eval.in/601261

The way you want is here:-

https://eval.in/601263

OR

https://eval.in/601279

Upvotes: 2

Jayesh Chitroda
Jayesh Chitroda

Reputation: 5049

To get unique values from array use array_unique():

$array = array(1,2,1,5,10,5,10,7,9,1) ;
array_unique($array);
print_r(array_unique($array));

Output:

Array
(
    [0] => 1
    [1] => 2
    [3] => 5
    [4] => 10
    [7] => 7
    [8] => 9
)

And to get duplicated values in array use array_count_values():

$array = array(1,2,1,5,10,5,10,7,9,1) ;
print_r(array_count_values($array));

Output:

Array
(
    [1] => 3 // 1 is duplicated value as it occurrence is 3 times
    [2] => 1
    [5] => 2 // 5 is duplicated value as it occurrence is 3 times
    [10] => 2 // 10 is duplicated value as it occurrence is 3 times
    [7] => 1
    [9] => 1
)

Upvotes: 1

Related Questions