Jump to content

[SOLVED] Ming sure no variables are the same


almightyegg

Recommended Posts

Is there any way to check if multipul variables are the same??

 

eg.

$1 = 11

$2 = 18

$3 = 9

$4 = 3

$5 = 3

 

instead of:

if(($1 == $2 && $1 != 0) || ($1 == $3 && $1 != 0) || ($1 == $4 && $1 != 0) || ($1 == $5 && $1 != 0) || ($2 == $3 && $2 != 0) ...etc....

 

*I also need to make sure that if they are the same that they aren't 0* <- reason for edit

 

is there a way to do it quickly and easily??

Hmmm, even the long way doesn't seem to work...

if(($item1 == $item2 && $item1 != 0) || ($item1 == $item3 && $item1 != 0) || ($item1 == $item4 && $item1 != 0) || ($item1 == $item5 && $item1 != 0) || ($item2 == $item3 && $item2 != 0) || ($item2 == $item4 && $item2 != 0) || ($item2 == $item5 && $item2 != 0) || ($item3 == $item4 && $item3 != 0) || ($item3 == $item5 && $item3 != 0) || ($item4 == $item5 && $item4 != 0)){
echo "You have selected the same item twice...or more!";
}else{
echo "Do stuff.....";
}

 

With the values I've set for item1,2,3,4,5 it should say "You have selected the same item twice...or more!" but it says "Do stuff....."

 

Is there a problem with the if?

Hope this helps, comments within

//Create an array of items
$array = array;

array_push($item01, $array);
array_push($item02, $array);
array_push($item03, $array);
array_push($item04, $array);

//Search through every item in the array
for(int $i=0; $i<count($array); $i++) {

     //Make a counter, initialize to 0 every "i" iteration
     $counter = 0;

     //Compare array[i] to every element in the array
     //if there is an element, increment counter by one
    //counter should always reach at LEAST one
     for(int $j=0; $j<count($array); $j++) {
         if($array[$i] == $array[$j]) {
             $counter++;
         }
     }

     //If there is 2 occurances, print out the item
     if($counter >= 2 && $array[$i] != 0) {
          print $array[$i] . " is in the array " . $counter . " times!";
    }
}

Warning: array_push() [function.array-push]: First argument should be an array in /home/lordofth/public_html/society/stockpile.php on line 175

 

Warning: array_push() [function.array-push]: First argument should be an array in /home/lordofth/public_html/society/stockpile.php on line 176

 

Warning: array_push() [function.array-push]: First argument should be an array in /home/lordofth/public_html/society/stockpile.php on line 177

 

Warning: array_push() [function.array-push]: First argument should be an array in /home/lordofth/public_html/society/stockpile.php on line 178

 

Warning: array_push() [function.array-push]: First argument should be an array in /home/lordofth/public_html/society/stockpile.php on line 179

 

 

$array = array();

array_push($item1, $array);
array_push($item2, $array);
array_push($item3, $array);
array_push($item4, $array);
array_push($item5, $array);

 

That's the problem area ^^^

This is kinda the ghetto way, but you could do this:

 

$arr = array(1,1,2,3,4,5,5,5,7,9);

$u_arr = array_unique($arr);

if(count($arr) != count($u_arr)) {
    //duplicates
    //it looked like you were trying to ignore duplicated 0s, but this doesn't allow for that
   //you would have to add some more code or possibly remove 0s before running this part x.x.
}

Right, it didn't work...

it just displayed:

Do stuff.....Do stuff.....Do stuff.....Do stuff.....Do stuff.....

 

Here's the code

$array = array();

array_push($array, $item1);
array_push($array, $item2);
array_push($array, $item3);
array_push($array, $item4);
array_push($array, $item5);

//Search through every item in the array
for($i=0; $i<count($array); $i++) {

     //Make a counter, initialize to 0 every "i" iteration
     $counter = 0;

     //Compare array[i] to every element in the array
     //if there is an element, increment counter by one
    //counter should always reach at LEAST one
     for($j=0; $j<count($array); $j++) {
         if($array[$i] == $array[$j]) {
             $counter++;
         }
     }

if($count1 != 1 && $item1 != 0){
echo "You don't have enough of at least one of the items you wanted to give.";
}elseif($count2 != 1 && $item2 != 0){
echo "You don't have enough of at least one of the items you wanted to give.";
}elseif($count3 != 1 && $item3 != 0){
echo "You don't have enough of at least one of the items you wanted to give.";
}elseif($count4 != 1 && $item4 != 0){
echo "You don't have enough of at least one of the items you wanted to give.";
}elseif($count5 != 1 && $item5 != 0){
echo "You don't have enough of at least one of the items you wanted to give.";
}elseif($counter >= 2 && $array[$i] != 0) {
          print $array[$i] . " is in the array " . $counter . " times!";
}else{
echo "Do stuff.....";
}
}

$array = array();

array_push($array, $item1, $item2, $item3, $item4, $item5);

//according to the php manual, array_push shouldn't be used to add 1 var at a time....  It probably shouldn't even be used here, but I'm lazy

$no_zero = array();
foreach($array as $v) {
     if($v != 0) $no_zero[] = $v;
}

if(count($no_zero) == count(array_unique($no_zero)) {
     //all unique
}
else {
    //not all unique
}

Parse error: syntax error, unexpected '{' in /home/lordofth/public_html/society/stockpile.php on line 196

 

$array = array();

array_push($array, $item1, $item2, $item3, $item4, $item5);

//according to the php manual, array_push shouldn't be used to add 1 var at a time....  It probably shouldn't even be used here, but I'm lazy

$no_zero = array();
foreach($array as $v) {
     if($v != 0) $no_zero[] = $v;
}

if($count1 != 1 && $item1 != 0){
echo "You don't have enough of at least one of the items you wanted to give.";
}elseif($count2 != 1 && $item2 != 0){
echo "You don't have enough of at least one of the items you wanted to give.";
}elseif($count3 != 1 && $item3 != 0){
echo "You don't have enough of at least one of the items you wanted to give.";
}elseif($count4 != 1 && $item4 != 0){
echo "You don't have enough of at least one of the items you wanted to give.";
}elseif($count5 != 1 && $item5 != 0){
echo "You don't have enough of at least one of the items you wanted to give.";
}else{

if(count($no_zero) == count(array_unique($no_zero)){ // line 196
     //all unique
echo "unique";
}else{
    //not all unique
echo "not unique";
}

}

 

Thanks loads for all this help ^_^

I got this far:

$rawr = array("$item1", "$item2", "$item3", "$item4", "$item5");
$arraycount = array_count_values($rawr);

 

How do I put it into an elseif??

 

<?php
$item1 = 4;
$item2 = 2;  
$item3 = 4;
$item4 = 6;  
$item5 = 9; 

$rawr = array($item1, $item2, $item3, $item4, $item5);
$arraycount = array_count_values($rawr);

foreach ($arraycount as $item => $count)
{
    if ($count > 1) echo "$item exists $count times<br>";
}
?>

Ohhhhh.....  Logic error on my part....

 

Because of the !=, php is converting the string to an integer, which is making the string 0, so it always equals 0.

 

Thus, no_zero is empty, and of course, the other function would return an empty array too.

 


$array = array();

array_push($array, $item1, $item2, $item3, $item4, $item5);

//according to the php manual, array_push shouldn't be used to add 1 var at a time....  It probably shouldn't even be used here, but I'm lazy

$no_zero = array();
foreach($array as $v) {
     if($v !== 0) $no_zero[] = $v;
     //for $v to be 0, it would have to be integral or maybe float 0....  In other words, a string '0' will still be added there
}

if(count($no_zero) == count(array_unique($no_zero)) {
     //all unique
}
else {
    //not all unique
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.