2017-04-24 97 views
0

在下面的代碼中,我使用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', 
) 

預期的電子郵件結果 - 按$valuesvar_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"; 
} 

回答

1

這是我通過電子郵件調試一些變量的方式。

mail($to,$subject,print_r($variable,true)); 

var_export打印對象,因爲此函數無法處理引用循環/遞歸數組。

這是一篇關於differences between var_export and print_r(和var_dump也很有用)的文章。

+0

謝謝,但它仍然給我單個結果 –

+0

哦,這是因爲你正在覆蓋$ values數組。 編輯的foreach: '的foreach($賓客$ K => $客人)' 然後,在$值: '$值[$ K] =陣列(...)' 這應該工作。 –

+0

正確我只是不能點擊/過度思考如何解決它 –

0

說實話,我不明白的問的是。請重新說明您希望預期的結果。

請查看代碼

$values = array(
    'FirstName' => $guest['FirstName'], 
    'LastName' => $guest['LastName'], 
    'Email' => $guest['email'], 
    'Country' => $guest['country'], 
    '_CheckIn' => $guest['check-in_date'], 
    '_CheckOut' => $guest['check-out_date'], 
); 

在的foreach這一節中,變量$值被完全與您剛剛創建的陣列取代。也許你打算做類似

$values[] = array(
    'FirstName' => $guest['FirstName'], 
    'LastName' => $guest['LastName'], 
    'Email' => $guest['email'], 
    'Country' => $guest['country'], 
    '_CheckIn' => $guest['check-in_date'], 
    '_CheckOut' => $guest['check-out_date'], 
); 

上面的代碼會將新數組添加到$ values數組中。

+0

我用當前和預期結果的示例更新了我的問題 –

+0

剛剛收到來自堆棧溢出的電子郵件,它看起來像我提供的信息可以幫助您。恭喜。 – Johnish