Sukhchain Singh
Sukhchain Singh

Reputation: 41

Cross check if multiple variable are equal in php

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'];

Upvotes: 3

Views: 2113

Answers (5)

Wizard
Wizard

Reputation: 862

$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);

Upvotes: 0

roberto06
roberto06

Reputation: 3864

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

Upvotes: 0

Manh Nguyen
Manh Nguyen

Reputation: 940

$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

Upvotes: 4

Ivan Velichko
Ivan Velichko

Reputation: 6709

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.

Upvotes: 1

RobertS
RobertS

Reputation: 135

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>';

    ?>

Upvotes: 0

Related Questions