【问题标题】:accessing array value in for loop [closed]在for循环中访问数组值[关闭]
【发布时间】: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 &lt;= $snd_cnt; $x++){ 替换 for 循环,它仍然会覆盖但保留 [1]
  • 您可能需要向我们展示“更多代码……..”您的代码。
  • @rizer123 已经用所有代码改写了这个问题,这是一个可以接受的问题吗?

标签: php arrays for-loop zen-cart


【解决方案1】:

您可以使用foreach 循环:

foreach ($emailArray as $key => $value) {
     $send_to_name = $value['name'];
     $send_to_email = $value['email'];  
}

【讨论】:

  • 那应该改变什么?无论哪种方式,它都应该工作。
  • 这并没有改变任何东西,完全一样,只是性能变慢了
  • 不是真的,它只会循环实际的索引。我认为 OP 代码会循环清除变量的额外时间,因为他有 &lt; 而不是 &lt;=
  • @Andreas 那是错误的。 OP 的代码很好,&lt;= 使用会出错并会发出通知!
  • 所以我得到这个错误:PHP 警告:当我使用上述代码时,为 foreach() 提供了无效参数。
【解决方案2】:

send_to_name 变量正在从数组中获取最新的元素。这是因为您一次又一次地将值分配给该变量。您可以使用 foreach 循环。

foreach ($emailArray as $key => $value) {
  $send_to_name = $value ['name'];
  $send_to_email = $value ['email'];
  //rest code to send email.
}

【讨论】:

  • 使用此代码时见上述错误
  • 您必须尝试回显这些值。在我写//其余代码的地方添加这行代码。回声 $send_to_name。" : "。 $send_to_email."
    ";
  • PHP 警告:为 foreach() 提供的参数无效是错误
  • 你的代码有问题。尝试将您的完整代码发布到描述中。您提供给我们的代码应该可以正常工作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-29
  • 1970-01-01
  • 2012-12-04
  • 1970-01-01
  • 2019-01-03
  • 1970-01-01
相关资源
最近更新 更多