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


How can input the variable in an array in this case?

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

这就是我想要实现的,但是,由于我需要变量而不是固定输入,我需要将其修改为字符串,这是代码:

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;
  );
第一部分是收集标签,意思是{名字},

{姓氏}等,标签是灵活的,所以意味着标签的数量不固定,$user[$tag['属性']]是我需要收集的值。如何检索值并将其放入数组中,就像我想要实现的那样?

目前它的错误

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

谢谢。

你的问题是你不能在 array(( 声明中使用 foreach(或任何类似的东西(。

改为以这种方式处理:

//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);
);