2

Hi lets say I've got this array:

$check_post = array(

 $_POST["a_post"],
 $_POST["b_post"],
 $_POST["c_post"],
 $_POST["d_post"],
 $_POST["e_post"],
 $_POST["f_post"],
 $_POST["g_post"],
 $_POST["h_post"],
 $_POST["i_post"]

 );

I want to check whether any elements of this array are repeated, so the best I got is this:

if (count(array_unique($check_post)) < count($check_post))  
    echo "Duplicate";  
else  
    echo "NO Duplicate";

Which works fine except for the fact that if more that one textarea is left blank (which is allowed) it gives me FALSE.

What I want is to NOT consider the empty values of the array for the (count(array_unique())

BTW I have tried with empty() and with array_values($check_post) but I cant get around it.

Thanks in advance!! please ask for any needed clarification.

4 Answers 4

10

To remove all the empty values from the comparison you can add array_diff():

if (count(array_unique(array_diff($check_post,array("")))) < count(array_diff($check_post,array(""))))  
Sign up to request clarification or add additional context in comments.

5 Comments

i think OP wants empties to contribute to the left quantity but not the right
Then he will always get "Duplicate" when there are empty values. I don't think that's what he wanted.
@jon I think OP wants empty keys to be ignored entirely
That is a simple and brilliant solution, this is exactly what I wanted. Thank you so much!!
oh that does make more sense. I read this "What I want is to NOT consider the empty values of the array for the (count(array_unique())" and took it literally
2

Well the way you have it is fine, though as you say, you have a need to remove the empty entries first.

$non_empty_check_post = array_filter($check_post, create_function('$item', 'return !empty($item);');

if (count(array_unique($non_empty_check_post)) < count($non_empty_check_post)) {
    echo "Duplicate";
} else {
    echo "NO Duplicate";
}

2 Comments

+1. But there's no need to supply a callback function to array_filter when you want to remove blanks (or entries that evaluate to false). Manual says "If no callback is supplied, all entries of input equal to FALSE will be removed." :)
@netcoder Very true! array_filter($check_post) would be sufficient.
2

Filter out the blanks from your array:

function no_blanks($val) {
    // Do not use empty() here if you don't consider the string "0" as blank
    return trim($val) !== '';
}

$check_post = array_filter($check_post, 'no_blanks');

if (count(array_unique($check_post)) < count($check_post))  
    echo "Duplicate";  
else  
    echo "NO Duplicate";

3 Comments

+1. But there's no need to supply a callback function to array_filter when you want to remove blanks (or entries that evaluate to false). Manual says "If no callback is supplied, all entries of input equal to FALSE will be removed." :)
@netcoder: My callback does a strict check for only the empty string, because strings containing the digit zero will be purged by array_filter() as well, which may go against OP's intentions. I did use array_filter() in another answer I posted earlier though :)
Ah, I see what you did here. Makes sense!
1
if (count(array_unique(array_filter(function(x) {return !empty(x)}, $check_post)) < count($check_post))  
    echo "Duplicate";  
else  
    echo "NO Duplicate";

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.