在PHP中处理关联数组和foreach

时间:2014-08-08 07:37:39

标签: php arrays for-loop multidimensional-array foreach

我正在尝试为我的邮件系统编写通用函数。

我打算做的是。

  1. 获取数组中的所有to-address值和所需信息
  2. 将所有值存储在数据库中。
  3. 我从二维关联数组开始,但我无法检索foreach中的值。

    最后,foreach echo语句应该像

    insert into my_table('name','to_address','subject','message','add_cc') values ('Author1','author1@gmail.com','Subject','Message','test@gmail.com');
    insert into my_table('name','to_address','subject','message','add_cc') values ('Author2','author2@gmail.com','Subject','Message','test@gmail.com');
    insert into my_table('name','to_address','subject','message','add_cc') values ('Author3','author3@gmail.com','Subject','Message','test@gmail.com');
    

    以下是我的代码,我知道问题在于foreach循环。即使关联数组最小化没有数字序列lik [0],[1],[2],我也会很高兴。

    <?php
    $to_address = array(
        array( 'Name' => "Author1", 'Email' => 'author1@gmail.com' ),
        array( 'Name' => "Author2",  'Email' => 'author2@gmail.com' ),
        array( 'Name' => "Author3", 'Email' => 'author3@gmail.com')
    );
    
    $subject = "Subject";
    $message = "Message";
    $add_cc  = "test@gmail.com";
    sendMail($to_address,$subject,$message,$add_cc);
    
    function sendMail($to_address,$subject,$message,$add_cc) {
        foreach ($to_address as $key => $val) {
            foreach ($val as $key1 => $val1) {
                print_r($val1);
            }
        }
    }
    
    ?>
    

    谢谢, Kimz

    编辑:

    换句话说,我无法循环$ to_address值,我需要帮助。

6 个答案:

答案 0 :(得分:1)

这是你想要的吗?您可以使用单个foreach并将每个数组放在INSERT

    $to_address = array(
        array( 'Name' => "Author1", 'Email' => 'author1@gmail.com' ),
        array( 'Name' => "Author2",  'Email' => 'author2@gmail.com' ),
        array( 'Name' => "Author3", 'Email' => 'author3@gmail.com')
    );

    $subject = "Subject";
    $message = "Message";
    $add_cc  = "test@gmail.com";
    sendMail($to_address,$subject,$message,$add_cc);

function sendMail($to_address,$subject,$message,$add_cc) {
          foreach ($to_address as $val) {
              $sql = "INSERT INTO my_table('name','to_address','subject','message','add_cc') 
                      VALUES ('".$val['Name']."','".$val['Email']."','".$subject."','".$message ."','".$add_cc."');";
              echo $sql . "<br>";
              //Use your Insert Statement
          }
}

答案 1 :(得分:1)

您可以在单个foreach中检索值:

function sendMail($to_address,$subject,$message,$add_cc){

    foreach ($to_address as $val) {
       $name = $val["Name"];
       $email = $val["Email"];
       echo "Name: $name, Email: $email <br\>\n";
    }
}

答案 2 :(得分:1)

先评论

  • 使用告诉我变量内部的名称。像$ val =&gt;这样的瘦身$ key告诉我什么。 foreach ( $val as $key1 => $val1 )
  • 都不会
  • 缩进:使用它,然后查看psr - *
  • 再次描述正在发生的事情。一个函数sendMail应该发送一封电子邮件。不打印查询

解决方案

$mailReceivers = array(
    array( 'Name' => "Author1", 'Email' => 'author1@gmail.com' ),
    array( 'Name' => "Author2",  'Email' => 'author2@gmail.com' ),
    array( 'Name' => "Author3", 'Email' => 'author3@gmail.com')
);

$subject = "Subject";
$message = "Message";
$add_cc  = "test@gmail.com";

foreach ( $mailReceivers as $receiver )
{
    parseMail($reciever['Name'], $reciever['Email'], $subject, $message, $add_cc);
}

function parseMail($name, $email, $subject, $message, $cc)
{
    print 'INSERT INTO my_table ('name','to_address','subject','message','add_cc') VALUES ('. $name .', '. $email .', '. $subject .', '. $message .', '. $subject .')';
}

说明

我调用了函数parseMail,因为我认为你不想打印一个insert语句。称之为你想要的。

你似乎也不知道foreach是如何运作的。玩一下它,看看$key => $val是什么意思。

我还将foreach从功能中移除。这使它更便于携带,更易于阅读。

答案 3 :(得分:1)

以下是您想要的代码 - 这将创建您需要的Insert查询。

<?php
$to_address = array( array( 'Name' => "Author1", 'Email' => 'author1@gmail.com' ),
array( 'Name' => "Author2",  'Email' => 'author2@gmail.com' ),
array( 'Name' => "Author3", 'Email' => 'author3@gmail.com')
);
$subject = "Subject";
$message = "Message";
$add_cc  = "test@gmail.com";
sendMail($to_address,$subject,$message,$add_cc);

function sendMail($to_address,$subject,$message,$add_cc){

    foreach ($to_address as $key => $val) {
        $sqlStatement =  "INSERT INTO my_table('name','to_address','subject','message','add_cc') VALUES ('".$val['Name']."','".$val['Email']."','".$subject."','".$message ."','".$add_cc."');";
        echo "$sqlStatement <br/>";
    }
}
?>

答案 4 :(得分:-1)

这是你想要的吗?

$link = mysqli_connect('localhost', 'my_user', 'my_password', 'my_db');

foreach ($to_address as $val) {

$sql = "insert into my_table('name','to_address','subject','message','add_cc') values (?,?,?,?,?)";
$stmt = mysqli_prepare($link, $sql);
mysqli_stmt_bind_param($stmt, $val['Name'], $val['Email'], $subject, $message, $add_cc);
);

mysqli_query($link, $sql);
}

答案 5 :(得分:-1)

   function sendMail($to_address,$subject,$message,$add_cc){

            foreach ($to_address as $key => $val) {

               extract($val);
               // use the keys of $val array as variable names 
               // http://php.net/manual/en/function.extract.php

               // sql for data base query
               $sql = "INSERT INTO my_table('name','to_address','subject','message','add_cc') VALUES ('".$Name."','".$Email."','".$subject."','".$message ."','".$add_cc."');";

               echo $sql;
            }
        }