...

View Full Version : Matching a variable to an associate array's key and retrieving key's value



nicky
11-18-2011, 02:20 PM
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:



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;



$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 :)

mlseim
11-18-2011, 02:29 PM
$to="abc";
$email=$to."@mycompany.com";

echo $email;

result ...
"abc@mycompany.com"

nicky
11-18-2011, 02:36 PM
That would work if the variable was a part of the email and all the email's domains were the same.



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

markspark100
11-18-2011, 02:53 PM
This should work:



if(array_key_exists($to, $contacts))
{
$email = $contacts[$to];
}


Mark

kbluhm
11-18-2011, 03:00 PM
What Mark has is a perfectly acceptable solution.

Small alternative though: isset() is much faster than array_key_exists(), especially on larger arrays:


$contacts = array(
'abc' => '123@yourbusiness.com',
'def' => 'noname@anotherdomain.com',
);

$to = 'abc';

if ( isset( $contacts[ $to ] ) )
{
$email = $contacts[ $to ];
}

markspark100
11-18-2011, 03:20 PM
What Mark has is a perfectly acceptable solution.

Small alternative though: isset() is much faster than array_key_exists(), especially on larger arrays:


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

kbluhm
11-18-2011, 03:29 PM
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:


$arr = array(
'foo' => NULL,
);

var_dump( array_key_exists( 'foo', $arr ) ); // TRUE

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

nicky
11-18-2011, 03:42 PM
Thanks kbluhm! Your code worked, but then I realized I need to add an extra layer to complicate things.



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

kbluhm
11-18-2011, 03:47 PM
Have a look at key (http://php.net/key)() and current (http://php.net/current)():


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 );
}

nicky
11-18-2011, 03:56 PM
Thank you so much!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum