3

Is there any efficient way to check these multiple variable values with each other?
Example: If value of $v1 is abc and value of $v2 is abc and other variables are empty then return error because two values are same.
Another Example: If value of $v1 is abc and value of $v4 is abc and other variables are empty then return error because two values are same.
Like check every variable with every other variable, if it's same with anyone then return an error.

$v1 = $_POST['v1'];
$v2 = $_POST['v2'];
$v3 = $_POST['v3'];
$v4 = $_POST['v4'];
$v5 = $_POST['v5'];
$v6 = $_POST['v6'];
0

5 Answers 5

4
$v1 = 1;
$v2 = 2;
$v3 = 3;
$v4 = 4;
$v5 = 5;
$v6 = 5;
$values = [$v1, $v2, $v3, $v4, $v5, $v6];
if (count($values) !== count(array_unique($values))) {
    echo 'Duplicates found';
}

DEMO

UPDATED

You can easily use array_count_values function to determine which values are duplicated. I put it here: http://phpio.net/s/16bh

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

4 Comments

@SukhchainSingh: If you name inputs name="v[]" then you will already have a $_POST['v'] array.
This will not work if two $_POST entries or more are empty. Check here
This sample is about to find duplicated values. I dot not care about the input. So you can still optimize code.
I'm just interpreting what the OP wrote : If value of $v1 is abc and value of $v2 is abc and other variables are empty then return error because two values are same.
1

Put them as keys to the assoc array and then check its len.

$vars = [];
$keys = ['v1', ..., 'v6'];
foreach ($keys as $k) {
    $vars[$_POST[$k]] = true;
} 

if (count($vars) != count($keys)) {
    echo 'Duplicates found!';
}

Explanation:

$_POST = [
    'v1' => 'abc',
    'v2' => 'cde',
    'v3' => 'abc',
]
$vars = [];
$keys = ['v1', 'v2', 'v3'];

// 1st foreach iteration:
$vars['abc'] = true;  // $vars = ['abc' => true];

// 2nd foreach iteration:
$vars['cde'] = true;  // $vars = ['abc' => true, 'cde' => true];

// 3rd foreach iteration:
$vars['abc'] = true;  // Again 'abc'! $vars is still ['abc' => true, 'cde' => true];

print_r(count($vars) == count($keys));  // 2 == 3

This algorithm has O(n) complexity while using array_unique() could be O(n*log(n)) if the last one uses sorting under the hood.

3 Comments

@SukhchainSingh, please look at the example code I've added.
i have checked but the length of all variables will be around 4-5 characters.
WTF is array_length ?
0

I´ve added comments, if you do not understand something feel free to ask, hope it will help you understand this :)

<?php
    $f1 = 'abc';
    $f2 = 'ac';
    $f3 = 'abc';

    $t1 = 'b';
    $t2 = 'a';
    $t3 = 'abc';

    // push to array
    $array_f[] = $f1;
    $array_f[] = $f2;
    $array_f[] = $f3;

    $array_t[] = $t1;
    $array_t[] = $t2;
    $array_t[] = $t3;

    // count($array_f) returns number of elements in array and array_unique removes duplicates from array
    // so if the length of original array is same as array without duplicates, then there weren´t duplicates else there were
    if(count($array_f) === count(array_unique($array_f))) echo 'array_t is unique return true</br>';
    else echo 'array_t is not unique return false</br>';

    if(count($array_t) === count(array_unique($array_t))) echo 'array_f is unique return true</br>';
    else echo 'array_ fis not unique return false</br>';

    ?>

Comments

0

I think the way to go here is to use array_unique :

<?php
$array = array($_POST['v1'], $_POST['v2'], $_POST['v3'], $_POST['v4'], $_POST['v5'], $_POST['v6']);
$array_unique = array_unique($array);

if ($array_unique == $array) {
    // No duplicates
} else {
    // duplicates
}
?>

EDIT : If you only want the $_POST values that are not empty :

<?php
$array = array();
if ($_POST['v1'] != '')
    $array[] = $_POST['v1'];
if ($_POST['v2'] != '')
    $array[] = $_POST['v2'];
if ($_POST['v3'] != '')
    $array[] = $_POST['v3'];
if ($_POST['v4'] != '')
    $array[] = $_POST['v4'];
if ($_POST['v5'] != '')
    $array[] = $_POST['v5'];
if ($_POST['v6'] != '')
    $array[] = $_POST['v6'];

if (count($array) < 2) {
    // Zero or one element in the array
} else {
    $array_unique = array_unique($array);

    if ($array_unique == $array) {
        // No duplicates
    } else {
        // duplicates
    }
}
?>

(Of course, the array building could be enhanced with a for loop)

Check it here : https://eval.in/683045

3 Comments

I've updated my answer to include the case in which only one value of $_POST is set.
it always returns a duplicate value.
It (the second code) works well, check the fiddle
0
$filtered = array_reduce(array_keys($_POST), function($carry, $key){
    // get all v[0..9]* variables from post
    if (preg_match('^/v\d+$/i',$key)) {
        $carry[$key]=$_POST[$key];
    }
    return $carry;
}, array());

$checked = array_unique($filtered);

$hasDuplicates = count($filtered) != count($checked);

another version (if you need to check exact POST-keys)

$keys = array('v1','v2','v3');
$filtered = array_intersect_keys($_POST, array_flip($keys));
$checked = array_unique($filtered);
$hasDuplicates = count($filtered) != count($checked);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.