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.

Trufa
  • 39,971
  • 43
  • 126
  • 190

4 Answers4

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(""))))  
AndreKR
  • 32,613
  • 18
  • 106
  • 168
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";
}
Orbling
  • 20,413
  • 3
  • 53
  • 64
  • 2
    +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 Nov 18 '10 at 20:19
  • @netcoder Very true! array_filter($check_post) would be sufficient. – Orbling Nov 18 '10 at 20:21
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";
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
  • +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 Nov 18 '10 at 20:20
  • @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 :) – BoltClock Nov 18 '10 at 20:22
  • Ah, I see what you did here. Makes sense! – netcoder Nov 18 '10 at 20:24
1
if (count(array_unique(array_filter(function(x) {return !empty(x)}, $check_post)) < count($check_post))  
    echo "Duplicate";  
else  
    echo "NO Duplicate";
jon_darkstar
  • 16,398
  • 7
  • 29
  • 37