Jump to content

Compare $_POST array to new array


davefootball123

Recommended Posts

Hi all. I have an HTML Input array seen below. I am using a php script to get the checked values. I am using the foreach method to get the values of the input array. How would I compare the values of the input array from the html to an array in my php script? My current php script is shown below. As you can see the array has the values of the input array...as well as the corresponding text value that would be echoed by my script. How would I compare the input array that I used $_POST['calltotor'] to retrieve to the array seen in the php script to get the corresponding value.


Any help would be great. Thanks,

 

<div id="tornado" style="display:none; height:auto; text-align:left;">
<input type="checkbox" name="calltotor[]" value="defaulttor"> Default Tornado Warning<br>
<input type="checkbox" name="calltotor[]" value="mobilehome"> Mobile Home<br>
<input type="checkbox" name="calltotor[]" value="motorists"> Motorists<br>
<input type="checkbox" name="calltotor[]" value="outside"> If Caught Outside<br>
<input type="checkbox" name="calltotor[]" value="basement"> Get to Basement<br>
<input type="checkbox" name="calltotor[]" value="lifethreatening"> Life Threatening Tornado<br>
<input type="checkbox" name="calltotor[]" value="extremelydangerous"> Extremely Dangersous and Life Threatening<br>
<input type="checkbox" name="calltotor[]" value="historyoftornado"> History of Producing A Tornado<br>
<input type="checkbox" name="calltotor[]" value="historyoftornadoes"> History of Producing Tornadoes<br>
<input type="checkbox" name="calltotor[]" value="historyofdamagingtornado"> History of Producing Damaging Tornado<br>
<input type="checkbox" name="calltotor[]" value="tornadoatnight"> Tornado at Night<br>
<input type="checkbox" name="calltotor[]" value="rainwrapped"> Rain Wrapped Tornado<br>
<input type="checkbox" name="calltotor[]" value="rainwrappedtornadowind"> Rain Wrapped Tornado In Addition To Destructive Winds<br>
<input type="checkbox" name="calltotor[]" value="supercell"> Supercell With All Severe Modes<br>
</div>
$callstoaction = array(
 
'defaulttor' => 'DEFAULT TORNADO WARNING',
  			
'mobilehome' => 'IF IN A MOBILE HOME ABANDON IT IMMEDIATELY',
  		
'motorists' => 'DO NOT SEEK SHELTER UNDER A HIGHWAY OVERPASS',

'outside' => 'IF CAUGHT OUTSIDE...',
  			
'basement' => 'THE SAFEST PLACE TO BE DURING A TORNADO IS IN A BASEMENT.',
  			
'lifethreatening' => 'THIS IS A LIFE THREATENING SITUATION...SEEK SHELTER NOW!',
  			
'extremelydangerous' => 'THIS IS AN EXTREMELY DANGEROUS AND LIFE-THREATENING SITUATION',
  			
'historyoftornado' => 'THIE STORM HAS THE HISTORY OF PRODUCING A TORNADO',
  			
'historyoftornadoes' => 'THIS STORM HAS THE HISTORY OF PRODUCING TORNADOES',
  			
'historyofdamagingtornado' => 'THIS STORM HAS THE HISTORY OF PRODUCING A DAMAGING TORNADO',
  			
'tornadoatnight' => 'TORNADOES ARE DIFFICULT TO SEE AND HEAR AT NIGHT...',
  			
'rainwrapped' => 'THIS TORNADO IS LIKELY WRAPPED IN RAIN...',
  			
'rainwrappedtornadowind' => 'IN ADDITION TO BRIEF RAIN-WRAPPED TORNADOES...',           
  			
'supercell' => 'THIS IS A SUPERCELL THUNDERSTORM CAPABLE OF ALL MODES OF SEVERE WEATHER.');





if ($_POST['warntype'] == 'TOR') {

foreach($_POST['calltotor'] as $toractions) {




        }
}

 

 

Link to comment
Share on other sites

I would have guessed that PHP would have a built in function to do an array_intersect using the values of one array against the indexes of another, but I don't see one. So, I see two options that would not require a foreach loop.

 

1. Use the POST array values to create an array using those as keys using array_fill_keys() and use that with array_intersect_key() to get the resulting array.

$selectedValuesAry = array_intersect_key($callstoaction, array_fill_keys($_POST['calltotor']));

 

2. To make things VERY simple you can just modify your checkboxes so you have ALL the information in the POST array. Use the keys from the $callstoaction array as the keys of the checkbox array and the values as the values:

 

<input type="checkbox" name="calltotor[defaulttor]" value="Default Tornado Warning"> Default Tornado Warning<br>
<input type="checkbox" name="calltotor[mobilehome]" value="Mobile Home"> Mobile Home<br>

 

The post value $_POST['calltotor'] will now have all the selected options and ther keys and values will be the same as the associated keys and values from the master array. You SHOULD be creating those checkboxes dynamically from the master array anyway, so this is by far the easiest and smartest option, in my opinion.

Edited by Psycho
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.