Why doesn't this work?!?

$i = 3;
foreach($contact as $key => $val) {
if($val['name'] == 'tom'){
echo $val['name']; // Tom
echo $val['email'][$i] //DOESN"T PRINT tom@foo3.bar
echo $val['email'][4]; //tom4@foo.bar
...

    Well, are you sure that $val['email'][3] is set and has the expected value of "tom@foo3.bar"? You might want to do a print_r($val); to find out what the array's actual contents are.

      It's not the value. I'm using a custom function to return either the email for question '2' the group emails for other questions. Echoing $fund[2]['email'][2] works but $i=2; echo $fund[2]['email'][$i] doesn't ??

      The post looks like

      $_POST[TrustFunds] => Array
              (
                  [0] => AGC-International Union of Operating Engineers 07
                  [1] => Alaska Carpenters Trust Funds 40
              )

      And the source array:

      $fund[] = array(
      	'num' => '07',
      	'name' => 'AGC-International Union of Operating Engineers',
      	'email' => array( 
      		0 => '',
      		1  => '',
      		2  => 'cat@wpas-inc.com',
      		3  => 'dog@wpas-inc.com',
      		4  => 'grape@wpas-inc.com',
      		5  => ''
      		)
      	);
      $fund[] = array(
      	'num' => '40',
      	'name' => 'Alaska Carpenters Trust Funds',
      	'email' => array( 
      		0 => '',
      		1  => '',
      		2  => 'foo@wpas-inc.com',
      		3  => 'foo@wpas-inc.com',
      		4  => 'bar@wpas-inc.com',
      		5  => ''
      		)
      	);

      The function is:

      function findrecipient()
      {
      		global $question, $fund;
      		if(isset($_POST['recipient']) && $_POST['recipient'] !== '') 
      		{
      				return $_POST['recipient'];
      		}
      		if(isset($_POST['ContactReason']) && !is_array($_POST['ContactReason']) && $_POST['ContactReason'] != '') 
      		{ // return an email for the one question
      				$qnum = substr(trim($_POST['ContactReason']), -2);
      				foreach($question as $key => $val) 
      				{
      						if($qnum == $val['num'] && $val['email'] != '') {
      								return $val['email']; 
      						}
      				}
      		}
      		if(isset($_POST['ContactReason']) && isset($_POST['TrustFunds']) && $_POST['TrustFunds'] != '') 
      		{ // if question[email] skipped return the fund emails
      				$holder = "";
      				foreach($_POST['TrustFunds'] as $key => $val) 
      				{ // form contains fund question, find the email for each fund
      						$fnum = substr(trim($val), -2);
      						foreach($fund as $key => $val) 
      						{ // match funds array and an email
      								if($fnum == $val['num']) && $val['email'][$qnum] != '') {
      
      									$holder[] =  $val['email'][$qnum];
      							}
      					}
      			}
      			if(is_array($holder)) 
      			{
      					return implode(', ', $holder);
      			}
      	}
      	// default return 
      	return 'webmaster@wpas-inc.com';
      }

      Thanks for any help!

        Write a Reply...