-1

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);
2
  • 2
    i want only string - show output you want Commented Jul 6, 2016 at 11:08
  • This question is missing its exact desired result. One array? Two arrays? Does the first contain all unique values? Or only values that occur once? Does the second array contain all values that didn't belong to the first array? Or must the second array also not contain duplicate values? Commented Aug 22, 2024 at 20:37

4 Answers 4

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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
)

1 Comment

I need which are the values is unique value and which are the value for duplicate how we can find
0
<?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;
    }
  }
?>

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.