只有一个数组结果显示外部foreach

时间:2017-04-24 21:45:02

标签: php arrays foreach

在下面的代码中,我使用var_export来记录导入的信息,但是电子邮件只显示foreach循环之外的$values变量中的一个对象。

我怎样才能达到预期效果呢?

帕兹:

$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";
}

2 个答案:

答案 0 :(得分:1)

这是我通过电子邮件调试一些变量的方式。

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

var_export正在打印对象,因为此函数无法处理引用循环/递归数组。

以下是关于differences between var_export and print_r的帖子(以及也很有用的var_dump)。

答案 1 :(得分: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将被您刚刚创建的数组完全替换。也许你打算做一些像

这样的事情
$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数组。