Null
Null

Reputation: 69

Check if variables contain identical value

I have problem to check either any of my variable contains identical value. Assume I have 8 variable with random generated lowercase alphabet only. For your info, this value don't need to be secure.

$variable1 = random_string();
$variable2 = random_string();
$variable3 = random_string();
$variable4 = random_string();
$variable5 = random_string();
$variable6 = random_string();
$variable7 = random_string();
$variable8 = random_string();

function random_string($length = 6) {
    $characters = 'abcdefghijklmnopqrstuvwxyz';
    $characters_length = strlen($characters);
    $random_string = '';
    for ($i = 0; $i < $length; $i++) {
        $random_string .= $characters[rand(0, $characters_length - 1)];
    }
    return $random_string;
}

I have seen this accepted answer at this question. My question, is there any simpler method to compare all this 8 variables if any of this variables have same or identical value? And if it do have same value, then regenerate another random string until none of it contain same value.

Upvotes: 0

Views: 283

Answers (3)

mega6382
mega6382

Reputation: 9396

If the number of variables is static then try:

$variable1 = random_string();
$variable2 = random_string();
$variable3 = random_string();
$variable4 = random_string();
$variable5 = random_string();
$variable6 = random_string();
$variable7 = random_string();
$variable8 = random_string();

function random_string($length = 6) {
    $characters = 'abcdefghijklmnopqrstuvwxyz';
    $characters_length = strlen($characters);
    $random_string = '';
    for ($i = 0; $i < $length; $i++) {
        $random_string .= $characters[rand(0, $characters_length - 1)];
    }
    return $random_string;
}
$arr = [];
for($i=1;$i<=8;$i++)
{
    if(!in_array(${"variable$i"}, $arr))
    {
        $arr[] = ${"variable$i"};
    }else
    {
        ${"variable$i"} = random_string();
        $i--;
    }
}

This will access all variables dynamically, and generate a new value for the var which value already exists. This works by storing all the values in an array 1 by 1, and comparing the new value with the one that already exists.

Upvotes: 1

Neodan
Neodan

Reputation: 5252

I think, that in this situation is better to use a one array (not eight separate variables), it's allow easily to check if generated string is not used already. With list function you can assign the data from array to separate variables.

Example:

<?php
$variables = [];
for($i = 0; $i < 8; $i++) {
    $randomString = random_string();
    if ($i > 0 && in_array($randomString, $variables))
        $i--;
    else
        $variables[$i] = $randomString;
}
list($variable1, $variable2, $variable3, $variable4, $variable5, $variable6, $variable7, $variable8) = $variables;

print_r([$variable1, $variable2, $variable3, $variable4, $variable5, $variable6, $variable7, $variable8]);

Upvotes: 1

e.dan
e.dan

Reputation: 7475

How about using array_unique() and count()?

$a = array();
array_push($a, random_string());
array_push($a, random_string());
array_push($a, random_string());
array_push($a, random_string());
array_push($a, random_string());
array_push($a, random_string());
array_push($a, random_string());
array_push($a, random_string());

$num_dups = count($a) - count(array_unique($a));
if ($num_dups == 0) {
  echo("all unique\n");
} else {
  echo("number of dups: " . $num_dups . "\n");
}

echo("create one dup\n");
$a[1] = $a[2];

$num_dups = count($a) - count(array_unique($a));
if ($num_dups == 0) {
  echo("all unique\n");
} else {
  echo("number of dups: " . $num_dups . "\n");
}

function random_string($length = 6) {
    $characters = 'abcdefghijklmnopqrstuvwxyz';
    $characters_length = strlen($characters);
    $random_string = '';
    for ($i = 0; $i < $length; $i++) {
        $random_string .= $characters[rand(0, $characters_length - 1)];
    }
    return $random_string;
}

Output is:

$ php uniq.php
all unique
create one dup
number of dups: 1

Upvotes: 1

Related Questions