almightyegg Posted December 24, 2007 Share Posted December 24, 2007 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?? Quote Link to comment https://forums.phpfreaks.com/topic/83049-solved-ming-sure-no-variables-are-the-same/ Share on other sites More sharing options...
almightyegg Posted December 24, 2007 Author Share Posted December 24, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/83049-solved-ming-sure-no-variables-are-the-same/#findComment-422435 Share on other sites More sharing options...
PFMaBiSmAd Posted December 24, 2007 Share Posted December 24, 2007 Put the values into an array and use array_count_values() to find out how many of each value there are. Quote Link to comment https://forums.phpfreaks.com/topic/83049-solved-ming-sure-no-variables-are-the-same/#findComment-422437 Share on other sites More sharing options...
almightyegg Posted December 24, 2007 Author Share Posted December 24, 2007 I got this far: $rawr = array("$item1", "$item2", "$item3", "$item4", "$item5"); $arraycount = array_count_values($rawr); How do I put it into an elseif?? Quote Link to comment https://forums.phpfreaks.com/topic/83049-solved-ming-sure-no-variables-are-the-same/#findComment-422446 Share on other sites More sharing options...
shlumph Posted December 24, 2007 Share Posted December 24, 2007 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!"; } } Quote Link to comment https://forums.phpfreaks.com/topic/83049-solved-ming-sure-no-variables-are-the-same/#findComment-422469 Share on other sites More sharing options...
almightyegg Posted December 24, 2007 Author Share Posted December 24, 2007 Wow thanks. I put it in but there's an unexpected T_VARIABLE on this line for(int $i=0; $i<count($array); $i++) { Quote Link to comment https://forums.phpfreaks.com/topic/83049-solved-ming-sure-no-variables-are-the-same/#findComment-422477 Share on other sites More sharing options...
p2grace Posted December 24, 2007 Share Posted December 24, 2007 Remove the int before $i=0 Quote Link to comment https://forums.phpfreaks.com/topic/83049-solved-ming-sure-no-variables-are-the-same/#findComment-422507 Share on other sites More sharing options...
almightyegg Posted December 24, 2007 Author Share Posted December 24, 2007 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 ^^^ Quote Link to comment https://forums.phpfreaks.com/topic/83049-solved-ming-sure-no-variables-are-the-same/#findComment-422509 Share on other sites More sharing options...
corbin Posted December 24, 2007 Share Posted December 24, 2007 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. } Quote Link to comment https://forums.phpfreaks.com/topic/83049-solved-ming-sure-no-variables-are-the-same/#findComment-422510 Share on other sites More sharing options...
corbin Posted December 24, 2007 Share Posted December 24, 2007 array_push($item1, $array); array_push($item2, $array); array_push($item3, $array); array_push($item4, $array); array_push($item5, $array); Those are backwards Try this with all of them: array_push($array, $item1); Quote Link to comment https://forums.phpfreaks.com/topic/83049-solved-ming-sure-no-variables-are-the-same/#findComment-422511 Share on other sites More sharing options...
almightyegg Posted December 24, 2007 Author Share Posted December 24, 2007 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....."; } } Quote Link to comment https://forums.phpfreaks.com/topic/83049-solved-ming-sure-no-variables-are-the-same/#findComment-422513 Share on other sites More sharing options...
corbin Posted December 24, 2007 Share Posted December 24, 2007 $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 } Quote Link to comment https://forums.phpfreaks.com/topic/83049-solved-ming-sure-no-variables-are-the-same/#findComment-422516 Share on other sites More sharing options...
almightyegg Posted December 24, 2007 Author Share Posted December 24, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/83049-solved-ming-sure-no-variables-are-the-same/#findComment-422522 Share on other sites More sharing options...
corbin Posted December 24, 2007 Share Posted December 24, 2007 Oh.... My bad.... Forgot a ).... if(count($no_zero) == count(array_unique($no_zero))){ Quote Link to comment https://forums.phpfreaks.com/topic/83049-solved-ming-sure-no-variables-are-the-same/#findComment-422523 Share on other sites More sharing options...
almightyegg Posted December 24, 2007 Author Share Posted December 24, 2007 It echoed unique when $item3 and $item4 both = dappp Quote Link to comment https://forums.phpfreaks.com/topic/83049-solved-ming-sure-no-variables-are-the-same/#findComment-422524 Share on other sites More sharing options...
Barand Posted December 24, 2007 Share Posted December 24, 2007 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>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/83049-solved-ming-sure-no-variables-are-the-same/#findComment-422527 Share on other sites More sharing options...
corbin Posted December 24, 2007 Share Posted December 24, 2007 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 } Quote Link to comment https://forums.phpfreaks.com/topic/83049-solved-ming-sure-no-variables-are-the-same/#findComment-422531 Share on other sites More sharing options...
almightyegg Posted December 24, 2007 Author Share Posted December 24, 2007 Great! It's all working! Thanks all who helped and merry christmas Quote Link to comment https://forums.phpfreaks.com/topic/83049-solved-ming-sure-no-variables-are-the-same/#findComment-422596 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.