【发布时间】:2016-10-29 22:44:27
【问题描述】:
我正在循环构建一个数组,下面的结果用于测试目的:
$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);
然后在一个事件上触发这个函数:
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 总是返回空
$send_to_name 总是返回空
所以问题是我无法访问循环中的 2 个变量。
如果我 echo $email_array[0]['email'] 对索引进行硬编码,它会给我结果 test1@test.com,但在我为索引放入变量 $email_array[$x]['email'] 的那一刻,结果为空。
谁能指出我正确的方向? 我什至尝试了一个while循环,它也失败了。
【问题讨论】:
-
无法重现问题。请确保向我们展示您的完整和真实的代码。
-
请让你的问题更清楚,我真的不明白你的代码有什么问题。如果您在此之前有一个变量,那么它每次都会被覆盖
-
您的代码将覆盖姓名和电子邮件。第一次是 email[0],第二次是 email[1],第三次是空的。我想如果你用
for ($x = 0; $x <= $snd_cnt; $x++){替换 for 循环,它仍然会覆盖但保留 [1] -
您可能需要向我们展示“更多代码……..”您的代码。
-
@rizer123 已经用所有代码改写了这个问题,这是一个可以接受的问题吗?
标签: php arrays for-loop zen-cart