2012-03-25 152 views
0
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; 
); 

第一部分是收集包含{FirstName},{LastName}等的標籤,這些標籤是靈活的,這意味着沒有固定數量的標籤,$ user [$ tag ['Attribute']]是我需要的值去收集。 我如何檢索值並將其放入數組中,就像我想實現的一樣?

目前,它擁有

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

謝謝你的錯誤。

+1

我不知道你想做什麼(你也許可以張貼輸入/輸出的例子嗎?)。但是你這樣做的方式是非常錯誤的。你不能在數組聲明中做一個foreach循環。這是您的錯誤消息中所說的語法錯誤。 – Tchoupi 2012-03-25 21:18:47

回答

2

你的問題是你不能在數組()聲明中使用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); 
); 
+0

對不起,但我發現它的一些不同之處 – 2012-03-25 23:42:42

+0

爲新版本,它會打印出Array([[email protected]] => Array()[[email protected]] => Array()),其中固定的版本是好的,但新的不是 – 2012-03-25 23:44:06

+0

你能定義「新版本」和「固定版本」嗎?對不起:) – haltabush 2012-03-25 23:57:00

相關問題