10

I know there is a lot of documentation around this but this one line of code took me ages to find in a 4000 line file, and I would like to get it right the first try.

file_put_contents($myFile,serialize(array($email_number,$email_address))) or die("can't open file");
    if ($address != "email@domain.com") {
        $email['headers'] = array('CC' => 'email@domain.com');
    }
}

After this if statement I basically want to add on

'BCC' => 'another_email@domain.com'

into the $email['headers'] array (so it adds it whether the if evaluates to true or not)

Tallboy
  • 12,847
  • 13
  • 82
  • 173
  • See this.. create array object and assign values in the array.. http://hiox.org/33288-create-array-copy-using-php.php – Vaishu May 30 '12 at 05:28
  • The line inside the if statement may not be the 'best' way (I really don't know) but it will work. $email['headers'] = array('BCC' => 'another_email@domain.com'); – Jake May 30 '12 at 05:51

3 Answers3

26

You can add them individually like this:

$array["key"] = "value";

Collectively, like this:

$array = array(
    "key"  => "value",
    "key2" => "value2"
);

Or you could merge two or more arrays with array_merge:

$array = array( "Foo" => "Bar", "Fiz" => "Buz" );

$new = array_merge( $array, array( "Stack" => "Overflow" ) );

print_r( $new );

Which results in the news key/value pairs being added in with the old:

Array
(
  [Foo] => Bar
  [Fiz] => Buz
  [Stack] => Overflow
)
Clive
  • 36,918
  • 8
  • 87
  • 113
Sampson
  • 265,109
  • 74
  • 539
  • 565
2

You can do this: $email['headers']['BCC'] = "Test@rest.com" but you need to add it after the if.

KillerX
  • 1,436
  • 1
  • 15
  • 23
0
$email['headers'] = array();

if ($address != "email@domain.com") {
   $email['headers']['CC'] = 'email@domain.com';
}

$email['headers']['BCC'] = 'another_email@domain.com';
flowfree
  • 16,356
  • 12
  • 52
  • 76