Jump to content

Compare value in array to key in another array.


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.


//This is my array.
$emails_array = array(
'Landscape Maintenance'=>'me@you.com',
'Landscape Design'=>'me@me.com',
'Planting Renovation'=>'me@me.com',
'Tree Service'=>'me@me.com',
'Weed Man Lawn Fertilization'=>'me@me.com',

//This grabs the post of the drop down box and inserts the email addy.


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: ';


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">
    <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>


I would appreciate any help on this. thank you.

Link to comment
Share on other sites

You should use array_intersect_key, combined with array_flip:

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

    [Landscape Maintenance] => me@me.com
    [Tree Service] => me@you.com
    [snowplowing] => me@you.com

Link to comment
Share on other sites

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


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


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.

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.

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.