jeffkab Posted October 25, 2007 Share Posted October 25, 2007 This is a question from a new PHP programmer! I've got a function that will gather up a list of snowboard tricks and numerically compare and match the trick ID with the rate of the wind and the snow conditions. If the trick numerical ID within the array is less than the numerical ID of the wind factor, then a statement is echoed, saying "The trick cannot be performed due to highwinds." etc. etc... This may be irrelevant to the question's answer but I just wanted to give a quick summary of what I'm trying to figure out. So I have the following code already written: $trick = array("trick1","trick2","trick3","trick4"); $wind = array("super windy","windy","breeze","still air"); My question is this: How do I formulate an IF/ELSE statement to check that if the wind's numerical ID within its array is less than the trick's numerical ID, it will echo something, and if else, it will go on to compare ANOTHER array with the $trick array. I'm hoping this may make at least SOME sense. Any help will be beneficial! Thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/74667-if-else-array-help/ Share on other sites More sharing options...
Wes1890 Posted October 25, 2007 Share Posted October 25, 2007 try: $trick = array("trick1" => 1,"trick2" => 2,"trick3" => 4,"trick4" => 5); $wind = array("super windy" => NUMBERS,"windy" => NUMBERS,"breeze" => NUMBERS,"still air" => NUMBERS); i write them like this though... same thing.. just easier to read $trick = array( "trick1" => 1, "trick2" => 2, "trick3" => 4, "trick4" => 5 ); $wind = array( "super windy" => NUMBERS, "windy" => NUMBERS, "breeze" => NUMBERS, "still air" => NUMBERS ); So when you echo $wind['super windy'] it will say "NUMBERS" like that.. just set each one to it's own int variable Quote Link to comment https://forums.phpfreaks.com/topic/74667-if-else-array-help/#findComment-377447 Share on other sites More sharing options...
kratsg Posted October 25, 2007 Share Posted October 25, 2007 For me, I'd prefer not adding anymore coding than needed, such as associative arrays. $trick = array("trick1","trick2","trick3","trick4"); $trick_two = array("trick5","trick6","trick7","trick8"); $wind = array("super windy","windy","breeze","still air"); $i=0; function check_arrays($array){ foreach($wind as $wind_factor=>$wind_type){ foreach($array as $trick_id=>$trick){ if($wind_factor < $trick_id){echo "$wind's numerical id is less than $trick's numerical id.<br>";} else if($wind_factor >= $trick_id && $i == 0){$i++;check_array($trick_two);} else {echo "$wind's numerical id is greater than $trick's numerical id.<br>";} } } } This would be my idea of what you said. First, we do a loop of the wind array, then, for each array value of the wind, we will loop the trick array, compare wind_factor versus trick_id. If wind_factor is less than trick_id, it echos. If it is greater than, it will hit your next array (adds a value to $i so you don't recall the same array again if it fails.) Quote Link to comment https://forums.phpfreaks.com/topic/74667-if-else-array-help/#findComment-377456 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.