我試圖將一些信息從數據庫傳遞到HTML以顯示給我的用戶。每當「行業」發生變化時,我都想打印新標籤。我試圖使用一個名爲$ last_industry的變量來追蹤我目前正在迭代的行業是否與最後一個行業相同,但是我沒有很好的結果。我在下面粘貼了我的代碼。 $ c ['user_industries_title']是我需要監控的。在foreach結尾處更改其值的變量()
$last_industry = 'foo';
foreach($case_studies as &$c) {
//If the last industry we iterated over is different than the one we're currently iterating over, close the last section and print a new section.
if($last_industry != $c['user_industries_title']){
echo "</section>"
echo "<section>";
$changed = 1;
}else {$changed = 0}
$c = $last_industry;
$last_industry = $c['user_industries_title'];
}
此問題與$ last_industry變量。爲了達到這個目的,它需要將自己更新到最新的$ c ['user_industries_title'],以便在下一次迭代開始時使用,這種情況沒有發生。我錯過了什麼嗎?
您在倒數第二行覆蓋'$ c'。 – Gareth