ageattack Posted September 4, 2014 Share Posted September 4, 2014 I want an if statement that not only checks for the existence of a variable, but which also checks all the variables already created for one with a specific value. For example, I have a loop which creates variables, but I don't want it to make two variables with the same value. The problem is, using if(isset(${'h'.$x}==false) && ${'h'.$x} != 3{ } won't work since the variable equal to 3 could be named h4, whereas this one will be named h5. I hoped I explained this efficiently enough, sorry about any confusion. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted September 4, 2014 Share Posted September 4, 2014 Can you show us this code? Not catching your statement "have a loop that creates variables". Quote Link to comment Share on other sites More sharing options...
requinix Posted September 4, 2014 Share Posted September 4, 2014 You're pretty close actually. But don't use variable variables. If you want arbitrary variables then you should be using an array with arbitrary keys instead. Then if(isset($array['h'.$x])==false && $array['h'.$x] != 3) { Quote Link to comment Share on other sites More sharing options...
ageattack Posted September 4, 2014 Author Share Posted September 4, 2014 (edited) $count = count($bytes); for($x=0; $x<$count; $x++){ ${'h' . $x} = substr($bytes[$x],8,-; $array1[${'h' . $x}] = array(); $array2[${'h' . $x}] = array(); $array3[${'h' . $x}] = array(); $array4[${'h' . $x}] = array(); $array5[${'h' . $x}] = array(); $offset[${'h' . $x}] = array(); $avg[${'h' . $x}] = array(); $avg2[${'h' . $x}] = array(); $percentage[${'h' . $x}] = array(); $values[${'h' . $x}] = null; } This is my code, where $bytes is an array containing the preg_match_all. The thing is, sometimes there is the same matchable string twice, meaning that it will create the same array key 2 times. Also, there will be two different variables, such as h2 and h13, which will have the same value. That doesn't screw the program up at all, but it makes a hell of a lot of error messages pop up. I just want to make sure that there are no $hx variables with the same value. P.S. I apologize if my code looks like crap. I'm still learning. Edited September 4, 2014 by ageattack Quote Link to comment Share on other sites More sharing options...
Solution Ch0cu3r Posted September 5, 2014 Solution Share Posted September 5, 2014 What is it you are trying to do? Variable variables inst efficient way of grouping data. That is what arrays are for. If you are only wanting to get unique values then store your data in an array and use array_unique to get only the unique values. Quote Link to comment 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.