-2

I am building an array in loop and the results below it for testing purposes:

$email_array[]= array(
            'email' => $fnd_result->fields['email'],
            'name' => $fnd_result->fields['name']
        );
 //$emailArray
 (
[0] => Array
    (
        [email] => test1@test.com
        [name] => test1 
    )

[1] => Array
    (
        [email] => test2@test.com
        [name] => test2  
    )
    )
 $snd_cnt = count($email_array);

then on an event this function is triggered:

 for ($x = 0; $x < $snd_cnt; $x++){
    $send_to_name = $email_array[$x]['name'];
    $send_to_email = $email_array[$x]['email']; 
    $email_subject = "Your Forklift Has Arrived!";
 // Prepare Text-only portion of message
    $text_message = OFFICE_FROM . "\t" . $bname . "\n" .
    'As you requested, we are notifying you that we have a new forklift in our inventory' . "\n" ."\n" .
    'We added a: ' . "\n" ."\n" .
    'Year: ' . $forklift_year . "\n" . 
    'Make: ' . $forklift_make . "\n" .
    'Model: ' . $products_model . "\n" .
    'Please visit our website at ojlforklifts.com or call us at (305) 836-4337 ' ."\n" ."\n" ."\n" .
    $extra_info['TEXT'];
// Prepare HTML-portion of message
    $html_msg['EMAIL_GREETING'] = $email_subject;
    $html_msg['EMAIL_WELCOME'] = 'We recieved a Forklift within your specs.';
    $html_msg['EMAIL_FIRST_NAME'] = $send_to_name;
    $html_msg['EMAIL_MESSAGE_HTML'] = '<table> ' .
    '<tr><td>Make: </td> <td>' . $forklift_make . '</td></tr>' .
    '<tr><td>Capacity: </td> <td>' . $forklift_capacity . 'Lbs</td></tr>' . 
    '<tr><td>Fuel: </td> <td>' . $fuelM . '</td></tr>' . 
    '<tr><td>Tires: </td> <td>' . $tireM . '</td></tr>' . 
    '<tr><td>Price: </td> <td>' . $products_price . '</td></tr>' . 
    '<tr><td colspan="2"><img src="http://ojlforklifts.com/images/' . $products_image . '" width="350"/></td></tr>' .
    '</table>';


  zen_mail($send_to_name, $send_to_email, $email_subject, $text_message, $name, $email_address, $html_msg ,'fork_notify');
}

$send_to_email always returns empty

$send_to_name always returns empty

So the problem is that i cant access the 2 variables in the loop. If I echo $email_array[0]['email'] with the index hardcoded, it will give me the result test1@test.com, but the minute i put put in a variable for the index, $email_array[$x]['email'], the result is empty.

Can anyone point me in the right direction? I even tried the a while loop and it fails also.

  • 1
    Can't reproduce the problem. Please make sure you show us your full and real code. – Rizier123 Jun 27 '16 at 19:49
  • Please make your problem more clear, I do not really understand what is wrong with your code. If you have a variable befor this it will be overwritten each time btw – JRsz Jun 27 '16 at 19:57
  • Your code will overwrite the name and email. First time it's email[0], second time it's email[1] and third time it will be empty. I think if you replace for loop with `for ($x = 0; $x <= $snd_cnt; $x++){` it will still overwrite but keep [1] – Andreas Jun 27 '16 at 19:58
  • You probably need to show us "More code…….." your code. – Rizier123 Jun 27 '16 at 20:06
  • @rizer123 have reworded the question with all the code, is this an acceptable question? –  Jul 07 '16 at 14:01

2 Answers2

2

You could use a foreach loop:

foreach ($emailArray as $key => $value) {
     $send_to_name = $value['name'];
     $send_to_email = $value['email'];  
}
Bram
  • 2,515
  • 6
  • 36
  • 58
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
  • And what should that change? It should work either way. – Rizier123 Jun 27 '16 at 19:57
  • This does not change anything at all, it is exact the same, only with a slower perfomance – JRsz Jun 27 '16 at 19:58
  • Not really, it will only loop the actual indexes. I think OPs code loops the extra time that clears the variables because he has `<` and not `<=` – Andreas Jun 27 '16 at 20:02
  • @Andreas And that is just wrong. OP's code is fine and `<=` would be wrong to use and would throw a notice! – Rizier123 Jun 27 '16 at 20:02
  • so i get this error : PHP Warning: Invalid argument supplied for foreach() when i use the above code. –  Jun 27 '16 at 20:18
  • this happen then is supplied to a foreach sometingh that are not array see this SO ..http://stackoverflow.com/questions/2630013/invalid-argument-supplied-for-foreach . which php version you are using ? – ScaisEdge Jun 27 '16 at 20:25
1

send_to_name variable is getting the latest element from the array. This because you assign the value again and again to this variable. You could use a foreach loop.

foreach ($emailArray as $key => $value) {
  $send_to_name = $value ['name'];
  $send_to_email = $value ['email'];
  //rest code to send email.
}
Bram
  • 2,515
  • 6
  • 36
  • 58
Rafael Shkembi
  • 786
  • 6
  • 16