我有一个比较2个数组的函数。 一个阵列由一组用户填充。 另一个数组来自一个公式。它包含一个ID列表。
如果ID与公式中的任何ID匹配,则应将用户保存为"已发布"。如果不是,它应该与释放不匹配。
我的公式如果我检查它们,但是当它们未经检查而因此不在数组中时,它们仍然保存为已发布。这是我的代码:
public function save_user_list(Requests $request)
{
$user_id_list = array();
$counter = 0;
if(Auth::user()->role == "owner"){
$users = User::where('owner_id',Auth::user()->owner_id)
->get();
}else{
$users = User::where('owner_id',Auth::user()->owner_id)
->where('role',"user")
->get();
}
foreach($users as $user)
{
$user_id_list[$counter] = $user->id;
$counter++;
}
for($i=0;$i<count($request->release);$i++)
{
$release = 0;
for($a=0;$a<$counter;$a++)
{
if($request->release){
if($request->release[$i] == $user_id_list[$a])
{
$release = 1;
}
}
}
$userrelease = User::where('id',$request->release[$i])
->where('owner_id',Auth::user()->owner_id)
->first();
if($userrelease)
{
$userrelease->active = $release;
$userrelease->save();
}
}
return back();
答案 0 :(得分:0)
修正了它。只是循环问题。
for ($a = 0; $a < $counter; $a++) {
$release = 0;
for ($i = 0; $i < count($request->release); $i++) {
if ($request->release) {
if ($request->release[$i] == $user_id_list[$a]) {
$release = 1;
}
}
}
$userrelease = User::where('id', $user_id_list[$a])
->where('owner_id', Auth::user()->owner_id)
->first();
if ($userrelease && $userrelease->role != "owner") {
$userrelease->active = $release;
$userrelease->save();
}
}