我試圖通過foreach循環向數組中添加值時遇到了問題。 基本上我有這種形式,其中有一堆話題,用戶可以點擊'喜歡'或'不喜歡'每個主題的兩個單選按鈕。然後我想要在兩個單獨的數組中收集「喜歡」和「不喜歡」,並將它們插入到數據庫中,但我做的不正確。 下面是從HTML代碼的示例:array_push無法在foreach循環中工作
Action movies Like<input type="radio" name="1" value="1" /> Dislike<input type="radio" id="2" name="1" value="2" />
而PHP代碼:
if (isset($_POST['submit'])) {
$likes = array();
$dislikes = array();
foreach($_POST as $key => $value) {
/* $key is the name of the object an user can click "like" or "dislike" on, $value
is either 'like', which is equal to '2' or 'dislike', equal to '1' */
if ($value > 1) { array_push($likes, $key); } else { array_push($dislikes, $key);
}
}
echo 'The object(s) the user likes: ' . $likes . ' ,
and the object(s) the user dislikes: ' . $dislikes;
我然而收到此:
「的對象(一個或多個)用戶喜歡:數組,以及用戶不喜歡的對象:陣列「
我敢肯定,這只是我在這裏犯的一個愚蠢的錯誤。我已經搜索了很多,但我一直沒有找到任何可以幫助我的解決方案。
爲什麼你使用的別名'加入()''主場迎戰破滅()'直接標識及名稱不能只有數字(或一個開始) – Shikiryu 2012-07-25 12:46:20