Jump to content

If... Else / Array HELP!!!


jeffkab

Recommended Posts

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!

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.