Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Regular Coder
    Join Date
    May 2008
    Location
    Michigan
    Posts
    216
    Thanks
    10
    Thanked 1 Time in 1 Post

    Matching a variable to an associate array's key and retrieving key's value

    I have a contact form that will send to different email addresses based on a variable. The variable is an employee's first initial and last name.

    Now I know I can program a bunch of conditionals saying:

    PHP Code:
    if($to == "abc") {
         
    $email "abc@abc.com";
         }

    if(
    $to == "def") {
        
    $email "def@def.com";
        } 
    But that could get lengthy if the list grows. What I'm trying to accomplish is to create an associative array such as;

    PHP Code:
    $contacts = array(
        
    "abc" => "abc@abc.com",
        
    "def" => "def@def.com"
        

    The trouble I'm having is matching the $to variable to a key in the array and retrieving that key's value.

    I've tried in_array, didn't work. I tried array_key_exists which found a matching key, but then I don't know how to retrieve the key's value.

    I would appreciate any help

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,538
    Thanks
    8
    Thanked 1,093 Times in 1,084 Posts
    $to="abc";
    $email=$to."@mycompany.com";

    echo $email;

    result ...
    "abc@mycompany.com"

  • #3
    Regular Coder
    Join Date
    May 2008
    Location
    Michigan
    Posts
    216
    Thanks
    10
    Thanked 1 Time in 1 Post
    That would work if the variable was a part of the email and all the email's domains were the same.

    PHP Code:
    $contacts = array(
         
    "abc" => "123@yourbusiness.com",
         
    "def" => noname@anotherdomain.com"
        ) 

  • #4
    New Coder
    Join Date
    May 2009
    Location
    Birmingham, England
    Posts
    59
    Thanks
    5
    Thanked 6 Times in 6 Posts
    This should work:

    PHP Code:
    if(array_key_exists($to$contacts))
       {
       
    $email $contacts[$to];
       } 
    Mark

  • #5
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    What Mark has is a perfectly acceptable solution.

    Small alternative though: isset() is much faster than array_key_exists(), especially on larger arrays:
    PHP Code:
    $contacts = array(
        
    'abc' => '123@yourbusiness.com',
        
    'def' => 'noname@anotherdomain.com',
    );

    $to 'abc';

    if ( isset( 
    $contacts$to ] ) )
    {
        
    $email $contacts$to ];


  • #6
    New Coder
    Join Date
    May 2009
    Location
    Birmingham, England
    Posts
    59
    Thanks
    5
    Thanked 6 Times in 6 Posts
    Quote Originally Posted by kbluhm View Post
    What Mark has is a perfectly acceptable solution.

    Small alternative though: isset() is much faster than array_key_exists(), especially on larger arrays:
    PHP Code:
    $contacts = array(
        
    'abc' => '123@yourbusiness.com',
        
    'def' => 'noname@anotherdomain.com',
    );

    $to 'abc';

    if ( isset( 
    $contacts$to ] ) )
    {
        
    $email $contacts$to ];

    Thanks, I've learned something new.

    I just did a quick test and isset came out at about 20% of the time for array_key_exists and that was just on the small array.

  • #7
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    The big difference you will see between the two, aside from speed, is that isset() will only acknowledge non-NULL values, where array_key_exists() acknowledges any key that exists:
    PHP Code:
    $arr = array(
        
    'foo' => NULL,
    );

    var_dumparray_key_exists'foo'$arr ) ); // TRUE

    var_dump( isset( $arr['foo'] ) ); // FALSE 

  • #8
    Regular Coder
    Join Date
    May 2008
    Location
    Michigan
    Posts
    216
    Thanks
    10
    Thanked 1 Time in 1 Post
    Thanks kbluhm! Your code worked, but then I realized I need to add an extra layer to complicate things.

    PHP Code:
    $contacts = array(
         
    "abc" => array(
              
    "Albert" => "albert@somedomain.com"
              
    ),
         
    "def" => array(
              
    "Doug" => "doug@anotherdomain.com"
              
    )
         ); 
    After retrieving the variable, I've designated that variable an actual name an email address. I need the name to echo and the email address to be used.

  • #9
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Have a look at key() and current():
    PHP Code:
    if ( isset( $contacts$to ] ) )
    {
        
    // grab the matching array
        
    $recip $contacts$to ];
        
    // the array key is the name
        
    $name  key$recip );
        
    // the array value is the email
        
    $email current$recip );


  • Users who have thanked kbluhm for this post:

    nicky (11-18-2011)

  • #10
    Regular Coder
    Join Date
    May 2008
    Location
    Michigan
    Posts
    216
    Thanks
    10
    Thanked 1 Time in 1 Post
    Thank you so much!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •