访问for循环中的数组值

时间:2016-06-27 19:47:59

标签: php arrays for-loop zen-cart

我正在循环中构建一个数组,并在其下面的结果用于测试目的:

$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循环,它也失败了。

2 个答案:

答案 0 :(得分:2)

您可以使用foreach循环:

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

答案 1 :(得分:1)

send_to_name变量从数组中获取最新元素。这是因为您一次又一次地为该变量赋值。你可以使用foreach循环。

// function call
uploadFiles(count($_FILES["upload"]["name"]), $_FILES["upload"]["tmp_name"], $_FILES["upload"]["name"], $path);

// function that uploads selected files
function uploadFiles($total, $tmpFiles, $uploadedFiles, $path) {
    for($i=0; $i < $total; $i++) {
        $tmpFilePath = $tmpFiles[$i];
        if ($tmpFilePath != ""){
            $newFilePath = "$path/" . $uploadedFiles[$i];
            if(file_exists($newFilePath)) {
                unlink($newFilePath);
            }
            move_uploaded_file($tmpFilePath, $newFilePath);
        }
    }
}