ThinkPHP間接修改重載元素無效

Notice:Indirect modification of overloaded element of app\api\model\Post has no effectapi

問題代碼:數組

首先,這個問題與實現ArrayAccess的全部類有關,它不是具體Post類的一個特殊問題。app

當您使用[]運算符從Post訪問元素時,它的行爲與數組不徹底相同。在內部它的offsetGet()方法被調用,並將在你的狀況下返回數組 – 但不返回該數組的引用。這意味着您對$data['comment']所作的全部修改都將丟失,除非您將其保存回來。code

解決方法:blog

新建一個變量存放$data['comment'],在遍歷賦值結束後,將變量值覆蓋$data['comment']。element

$cur = $data['comment'];
	foreach($cur as $k=>$v){
	
	}
	$data['comment'] = $cur;
相關文章
相關標籤/搜索