Jump to content

Compare value in array to key in another array.


TwiztedCupid

Recommended Posts

Another noob question here. I've got a form that I use to collect info for an estimate. I also am using Developer toolbox from Adobe. Makes my life a lot easier, but doesn't teach you anything. lol

 

Originally I had a drop down box that would hold the value of selected item and post it into to emailTO field. Which worked just fine.

Example:

//This is my array.
$emails_array = array(
'Landscape Maintenance'=>'[email protected]',
'Landscape Design'=>'[email protected]',
'Planting Renovation'=>'[email protected]',
'Tree Service'=>'[email protected]',
'Weed Man Lawn Fertilization'=>'[email protected]',
'Snowplowing'=>'[email protected]',
'Other'=>'[email protected]');

//This grabs the post of the drop down box and inserts the email addy.
$emailObj->setTo($emails_array[$_POST['Services']]);

 

Now I have to change the drop down box to a check box group. In which the user can select multiple services and have multiple email sent out if necessary. This is where I'm stuck. I need to get the post values compare the results to the key in $emails_array then spit out the email associated with it.

Here's what I got so far.

 

$postresult = $_POST['services'];
echo 'Postresult:  ' . "$postresult" . "<br />";
$myarray = explode(",", $postresult);
echo 'Dirty Array: ';
print_r($myarray);

 

Here are the results

Postresult:  Landscape Maintenance,Tree Service,Snowplowing<br>
MyArray: Array
(
    [0] => Landscape Maintenance
    [1] => Tree Service
    [2] => Snowplowing
)

 

How do I take either $postresult OR $myarray and compare it to $emails_array and get the unique email values for it? I want to maintain the original $postreault so i can insert it into a mysql db.

 

Here is the code Developer toolbox produces for the checkbox group.

<form method="post">
  <label>
    <input type="submit" name="Submit" id="Submit" value="Submit">
    <input name="services" id="services" wdg:recordset="Recordset1" wdg:subtype="CommaCheckboxes" wdg:type="widget" wdg:displayfield="service" wdg:valuefield="service" wdg:groupby="2" style="display: none; "><span><table cellpading="0" cellspacing="0" border="0"><tbody><tr><td><label id="services_label0" htmlfor="services_commacheckbox0" for="services_commacheckbox0"><input type="checkbox" id="services_commacheckbox0" value="Landscape Maintenance" cbFor="services">Landscape Maintenance</label></td><td><label id="services_label1" htmlfor="services_commacheckbox1" for="services_commacheckbox1"><input type="checkbox" id="services_commacheckbox1" value="Landscape Design" cbFor="services">Landscape Design</label></td></tr><tr><td><label id="services_label2" htmlfor="services_commacheckbox2" for="services_commacheckbox2"><input type="checkbox" id="services_commacheckbox2" value="Planting Renovation" cbFor="services">Planting Renovation</label></td><td><label id="services_label3" htmlfor="services_commacheckbox3" for="services_commacheckbox3"><input type="checkbox" id="services_commacheckbox3" value="Tree Service" cbFor="services">Tree Service</label></td></tr><tr><td><label id="services_label4" htmlfor="services_commacheckbox4" for="services_commacheckbox4"><input type="checkbox" id="services_commacheckbox4" value="Weed Man Lawn Fertilization" cbFor="services">Weed Man Lawn Fertilization</label></td><td><label id="services_label5" htmlfor="services_commacheckbox5" for="services_commacheckbox5"><input type="checkbox" id="services_commacheckbox5" value="Snowplowing" cbFor="services">Snowplowing</label></td></tr><tr><td><label id="services_label6" htmlfor="services_commacheckbox6" for="services_commacheckbox6"><input type="checkbox" id="services_commacheckbox6" value="Other" cbFor="services">Other</label></td><td colspan="2"> </td></tr></tbody></table></span>
  </label>
</form>

 

I would appreciate any help on this. thank you.

You should use array_intersect_key, combined with array_flip:

<?php
$selected = array_intersect_key($emails_array, array_flip($my_array));
/* Results are as follows: (according to your input last post)

array(
    [Landscape Maintenance] => [email protected]
    [Tree Service] => [email protected]
    [snowplowing] => [email protected]
)
?>

johnny86's would work.  At the same time, I was posting:

 

$emailsToSend = array();
foreach( $myarray as $service)
{
   $e = $emails_array[$service];
   if($e)
       $emailsToSend[$service]=$e;
}

 

Does this do what you need?

 

What can also do with your checkboxes is create an array in the post using a specific value  ( ;  the value does not have to be the string 'on' ).

 

<input type="checkbox" name="services[]" value="Landscaping" />
<input type="checkbox" name="services[]" value="Blah" />

 

That'll post $_POST['services'] as an array instead of a string.

Archived

This topic is now archived and is closed to further replies.

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