我試圖在每次 for 循環迭代后恢復所有數組項。我需要設置 Laravel 驗證Rule::notIn($arry)我有一個由克隆輸入字段生成的數組station_id。我想檢查所有克隆的車站 ID 是否唯一,因此請確保地鐵路線中沒有重復的車站。對于克隆字段,我通過計算克隆項目來設置使用 for 循環的規則。所以問題是,我想使用 `Rule::notIn($stationIds) 除了當前迭代項目 id,這樣我就可以通過檢查當前 id 不在數組項目的其余部分中來進行驗證。public function rules(){ // getting all input fields value $rStationIds = $this->get('station_id') ... // get the max number of input $counter = $this->getMaxCount($rStationIds, ...); $rules = []; // loop through each item for ($r = 0; $r < $counter; $r++) { unset($rStationIds[$r]); $rules['station_id'][$r] = ['required', 'int', Rule::notIn($rStationIds)]; } ...}上面代碼中的問題是,當我訪問unset($var)當前項目時,它永遠不會重置回原始數組元素;因此,最后一個字段將沒有任何內容可比較,因為數組將變空。我也可以使用任何其他方法來檢查克隆站 ID 字段的唯一項目。
1 回答

手掌心
TA貢獻1942條經驗 獲得超3個贊
將循環更改為:
// loop through each item
for ($r = 0; $r < $counter; $r++) {
$temp = $rStationIds[$r];
unset($rStationIds[$r]);
$rules['station_id'][$r] = ['required', 'int', Rule::notIn($rStationIds)];
$rStationIds[$r] = $temp;
}
- 1 回答
- 0 關注
- 100 瀏覽
添加回答
舉報
0/150
提交
取消