2013-01-31 25 views
0

我正在使用Twilio API發送短信。我試圖改變它,以便我可以從mysql結果發送到收件人列表。如何用MySQL中的數據填充PHP數組?

給出的示例代碼:

$people = array(
    "+14155551212" => "First Lastname", 
); 

我的代碼是:

$people = array(
    while($res = mysql_fetch_array($usersphone)) { 
    $people[$res['UserMobile']] = $res['UserFirstName']; 
    } 
); 

語法不好,但我想不通的地方。

+0

「語法不好」 - 爲什麼你認爲它是? – zerkms

+0

這不是'array()'的工作方式......你不能在'array'構造函數中嵌入'while'循環。 – Crontab

+0

您不能將一個while循環添加到數組中。將第一行改爲'$ people = array();'並移除');'在最後,這可能會有所幫助 – Class

回答

0

您不能將控制結構放入數組中。

$people = array(); 
while ($res = mysql_fetch_array($usersphone)) { 
    $people[$res["UserMobile"]] = $res["UserFirstName"]; 
}; 

此外,還有一噸這裏的帖子上,這樣會告訴你所有關於不使用mysql_*功能了,因爲他們是不贊成的。

0

你的數組定義中有邏輯。你應該定義數組,然後用它填充它。

// define the array 
$people = array(); 
while($res = mysql_fetch_array($usersphone)) { 
    // populate key with mobile and value with name 
    $people[$res['UserMobile']] = $res['UserFirstName']; 
} 
+0

非常感謝。我對定義數組並填充它或將邏輯合併到數組中感到困惑。你的回答幫助我理解了這一點。 – user2030605