Jess McKenzie
Jess McKenzie

Reputation: 8385

Only one array result showing outside foreach

In the code below I am using var_export to document the information being imported, However the e-mail is only showing one object within the $values variable outside of the foreach loop.

How can I make it so that I can get my expected result?

Pauls:

$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'],
  );

E-Mail Result:

array (
  'FirstName' => 'Test 1',
  'LastName' => 'Test 2',
  'Email' => '',
  'Country' => 'New Zealand',
  '_CheckIn' => '2017-04-25',
  '_CheckOut' => '2017-04-27',
)

Expected E-mail Result - as per $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',
)

Code:

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

Upvotes: 0

Views: 99

Answers (2)

Paul Soberanes
Paul Soberanes

Reputation: 56

This is the way I debug by email some variable.

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

var_export is printing the object because this function can not handle reference cycles/recursive arrays.

Here is a post about the differences between var_export and print_r (and var_dump that is useful too).

Upvotes: 1

Johnish
Johnish

Reputation: 81

To be honest, I don't understand what the ask is. Please rephrase what you would like the expected outcome to be.

Please review this section of code

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

On the foreach, the variable $values is being completely replaced with the array that you just created. Perhaps you meant to do something like

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

The above code will add the new array to the $values array.

Upvotes: 0

Related Questions