duanlisha2335 2012-03-25 21:08
浏览 15
已采纳

在这种情况下如何在数组中输入变量?

    foreach ($result as $user) {
  $replacements[$user['Email']] = array(
    '{FirstName}'=>$user['FirstName'],
    '{LastName}'=>$user['LastName'],
    '{Code}'=>$user['RandomCode']
  );
}

That is what i want to achieve , however, since i need variable instead of fixed input , i need to modify it to be a string, here is the code:

try{
$sql =
    'SELECT  *
    FROM     require_attributes
    where ListID=? 
    ';
$stmt = $conn->prepare($sql);
$stmt->execute(array($list));
$tagSet= $stmt->fetchAll();

}
catch(PDOException $e)
    {
    die ($e->getMessage().'<a href="mail/campaign_view.php"> Back</a>'); 
    }




try{
$sql =
    'SELECT  s.*
    FROM     subscriber s ,list_sub ls
    where ls.ListID=? 
    AND ls.SubID=s.SubID
    ';
$stmt = $conn->prepare($sql);
$stmt->execute(array($list));
$resultSub= $stmt->fetchAll();
}
catch(PDOException $e)
    {
    die ($e->getMessage().'<a href="mail/campaign_view.php"> Back</a>'); 
    }


//Put the result in array
foreach ($resultSub as $user) {
  $replacements[$user['Email']] = array(
  foreach ($tagSet as $tags)
  {$string=$string."'".$tags['Attribute']."'"."=>".$user[$tag['Attribute']].",";}
   $string = substr($string, 0, -1);
  echo $string;
  );

The first part is to collect the tags which means {FirstName}, {LastName} and so on , The tags are flexible so that means there are not fixed number of tag, the $user[$tag['Attribute']] is the value i need to collect. How can i retrieve the value and put it into the array just like what i want to achieve?

Currently it has an error of

Parse error: syntax error, unexpected T_ECHO, expecting ')' in C:\xampp\htdocs\fyp\mail\sendPersonal.php on line 186

Thank you.

  • 写回答

1条回答 默认 最新

  • douhua9726 2012-03-25 21:19
    关注

    Your problem is that you can't use a foreach (nor any thing like that) in a array() declaration.

    Process this way instead:

    //Put the result in array
    
    foreach ($resultSub as $user) {
      $replacements[$user['Email']] = array();
      foreach ($tagSet as $tags)
      {
           $replacements[$user['Email']]['{'.$tags['Attribute'].'}']=$user[$tags['Attribute']];
      }
      echo print_r($replacements[$user['Email']], true);
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比