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

    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.

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