在下面的代碼中,我使用var_export
來記錄正在導入的信息,但電子郵件僅顯示foreach循環外的$values
變量中的一個對象。只有一個數組結果顯示外部foreach
我該怎麼做才能獲得預期的效果?
保羅:
$values = [];
foreach ($guests as $k => $guest)
{
$values[$k] = array(
'FirstName' => $guest['FirstName'],
'LastName' => $guest['LastName'],
'Email' => $guest['email'],
'Country' => $guest['country'],
'_CheckIn' => $guest['check-in_date'],
'_CheckOut' => $guest['check-out_date'],
);
電子郵件結果:
array (
'FirstName' => 'Test 1',
'LastName' => 'Test 2',
'Email' => '',
'Country' => 'New Zealand',
'_CheckIn' => '2017-04-25',
'_CheckOut' => '2017-04-27',
)
預期的電子郵件結果 - 按$values
var_dump
array (
'FirstName' => 'Test 1',
'LastName' => 'Test 2',
'Email' => '',
'Country' => 'New Zealand',
'_CheckIn' => '2017-04-25',
'_CheckOut' => '2017-04-27',
)
array (
'FirstName' => 'Test 1',
'LastName' => 'Test 2',
'Email' => '',
'Country' => 'New Zealand',
'_CheckIn' => '2017-04-25',
'_CheckOut' => '2017-04-27',
)
array (
'FirstName' => 'Test 1',
'LastName' => 'Test 2',
'Email' => '',
'Country' => 'New Zealand',
'_CheckIn' => '2017-04-25',
'_CheckOut' => '2017-04-27',
)
代碼:
$values = [];
foreach ($guests as $guest)
{
$values = array(
'FirstName' => $guest['FirstName'],
'LastName' => $guest['LastName'],
'Email' => $guest['email'],
'Country' => $guest['country'],
'_CheckIn' => $guest['check-in_date'],
'_CheckOut' => $guest['check-out_date'],
);
//inserting guestst to Contact table
$optInEmail = $values['Email'];
$optInReason = 'Purchased Accommodation';
$tagID = '566'; //
$contactID = $infusionsoft->contacts()->addWithDupCheck($values,'Email');
$infusionsoft->contacts()->addToGroup($contactID, $tagID);
$infusionsoft->emails()->optIn($optInEmail, $optInReason);
echo '<pre>';
var_dump($values);
echo "</pre>";
}
if(empty($values))
{
echo '<h1>No new Data</h1>';
}else{
$from = "API";
$to = "";
$subject = "API Import"." ".date("h:i:sa")." ".date("d-m-Y");
$message = var_export($values, true);
$headers = "From:" . $from;
if(mail($to,$subject,$message, $headers))
{
echo '<h1>E-mail Sent Data Imported</h1>';
}else{
echo "What Broke Boss";
}
謝謝,但它仍然給我單個結果 –
哦,這是因爲你正在覆蓋$ values數組。 編輯的foreach: '的foreach($賓客$ K => $客人)' 然後,在$值: '$值[$ K] =陣列(...)' 這應該工作。 –
正確我只是不能點擊/過度思考如何解決它 –