1

I was working on something earlier today and I've stumbled upon this problem. How do you check if a certain array value is unique within that array?

$array = array(1, 2, 3, 3, 4, 5);

if(unique_in_array($array, 1)) // true
if(unique_in_array($array, 3)) // false

I've been thinking about using array_search() or in_array(), but neither is very useful for finding duplicates. I'm sure I could write a function like this to do it:

function unique_in_array($arr, $search){
    $found = 0;

    foreach($arr as $val){
        if($search == $val){
            $found++;
        }
    }

    if($found > 1){
        return true;
    } else {
        return false;
    }
}

Or another solution was to use array_count_values() like this:

$array_val_count = array_count_values($array);

if($array_val_count[$search] > 1){
    return true;
} else {
    return false;
}

But it seems odd to me that PHP does not have any built-in function (or at least a better way) to do this.

0

3 Answers 3

4

Try this:

if (1 === count(array_keys($values, $value))) {
    // $value is unique in array
}

For reference, see array_keys:

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

Comments

1

You can try it like this:

$array1 = array(1, 2, 3, 3, 4, 3, 3, 5);

$func  = array_count_values($array1);
$count = $func[3];  # Pass value here

echo $count;  # This will echo 4

# If you pass an undefined value, you should use it like as below
$count = isset($func[8])? $func[8] : 0;

echo $count;  # This will echo 0, because 8 is absent in $array1

Here is the function reference of array_count_values().

5 Comments

This is not very helpfull. You're checking if the array has duplicates or not. The question was to check if a certain value is unique or not.
Ahh! Yes! I am updating my answer! @icecub
Yes this is what I had in my question as well. I know it works, but I was looking for a more elegant solution like localheinz provided. I'll give you a +1 for efford though. Thanks for trying :)
But in this solution, if echo == 1, That mean Value is unique in the array. @icecub
Yes I know. But instead of simply going through the array once, you're creating another array out of the first one and start comparing the value. It's a lot of overhead that isn't needed.
-1

I have found this to check if an array has duplicate values:

$array = array(1, 2, 3, 3, 4, 5);

if(count(array_unique($array)) != count($array)){
    // Return true Array is unique
}
else{
    // Return false Array is not unique
}

2 Comments

Welcome to StackOverflow. While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply. Have a look here → How do I write a good answer?
Thanks for trying, but please read the question again. The question was if a specific value in an array is unique. Not if the entire array has unique values. So in an array like [0, 0, 1, 2, 2] it should return true for checking out 1 and false for all the others. @FedericoBaù This question is really old (3 / 4 years) and answered a long time ago. I honestly couldn't even remember asking it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.