[英]How do i push a new key value pair to an array php?
我知道有很多關於這方面的文檔,但這一行代碼花了我很多年才找到4000行文件,我想在第一次嘗試時做到正確。
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');
}
}
在這個if語句后我基本上想要添加
'BCC' => 'another_email@domain.com'
進入$email['headers']
數組(因此無論if是否為true,它都會添加它)
您可以像這樣單獨添加它們:
$array["key"] = "value";
總的來說,像這樣:
$array = array(
"key" => "value",
"key2" => "value2"
);
或者您可以使用array_merge
合並兩個或多個數組:
$array = array( "Foo" => "Bar", "Fiz" => "Buz" );
$new = array_merge( $array, array( "Stack" => "Overflow" ) );
print_r( $new );
這導致新舊鍵/值對被添加到舊:
Array ( [Foo] => Bar [Fiz] => Buz [Stack] => Overflow )
你可以這樣做: $email['headers']['BCC'] = "Test@rest.com"
但你需要在if之后添加它。
$email['headers'] = array();
if ($address != "email@domain.com") {
$email['headers']['CC'] = 'email@domain.com';
}
$email['headers']['BCC'] = 'another_email@domain.com';
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.