Php 从Laravel会话数组中删除项目
我正试图从Laravel会话数组中删除项目,但到目前为止运气不佳 我使用Session::push()方法将值存储在会话数组中: 我可以像普通数组一样访问值:Php 从Laravel会话数组中删除项目,php,arrays,session,laravel,Php,Arrays,Session,Laravel,我正试图从Laravel会话数组中删除项目,但到目前为止运气不佳 我使用Session::push()方法将值存储在会话数组中: 我可以像普通数组一样访问值: @for($i=0;$i<Session::get('dates');$i++) <tr> <td>{{ Session::get('event_date_display')[$i] }}</td> <td&
@for($i=0;$i<Session::get('dates');$i++)
<tr>
<td>{{ Session::get('event_date_display')[$i] }}</td>
<td>{{ Session::get('event_start_display')[$i] }}</td>
<td>{{ Session::get('event_entry_display')[$i] == '' ? '-' : Session::get('event_entry_display')[$i] }}</td>
<td>{{ Session::get('event_end_display')[$i] == '' ? '-' : Session::get('event_end_display')[$i] }}</td>
<td><a href="{{URL::route('termin-loeschen', $i)}}" class="del"><span class="icon-spam"></span>Loeschen {{ $i }}</a></td>
</tr>
@endfor
@for($i=0;$i在我看来,您试图从存储在会话中的数组中删除一个值。为此,我认为您需要执行标准数组取消设置($array[$key])
因此,我认为这类似于unset(Session::$array[$key]);
。当您调用Session::forget('event_data_display')[$index]
时,$index
变量不会被传递到forget()
method。这样,Laravel将看不到它,并将取消设置会话数组的整个“事件数据显示”索引
要取消设置该索引处的值,可能需要执行以下操作:
$event_data_display = Session::get('event_date_display');
unset($event_data_display[$index]);
Session::set('event_data_display', $event_data_display);
Laravel的会话不支持通过以下指定索引添加到阵列:
Session::push('user.teams', 'developers');
因此,您也可以访问该数组的索引,如下所示:
Session::forget('event_data_display.' . $i);
我没试过,但值得一试
当调用Session::get('event\u data\u display')[$i]
时,之所以有效,是因为PHP在查找存储在$i
索引中的值之前从Session::get('event\u data\u display')
检索数组值
调用Session::forget('event_data_display')
时,forget()
方法只能对传递给它的内容进行操作。只需使用下面的代码即可删除laravel中的所有会话
$request->session()->flush();我使用以下代码解决它。只需将要在数组中删除的值作为“id”
传递给控制器,如下面的代码所示
public function removeAddTocart(Request $request)
{
$remove = ''.$request->id.'';
if (Session::has('productCart'))
{
foreach (Session::get('productCart') as $key => $value)
{
if ($value === $remove)
{
Session::pull('productCart.'.$key); // retrieving the value and remove it from the array
break;
}
}
}
}
加
清除会话后。然后只删除会话中的值。$event\u data\u display=session::get('event\u date\u display');unset($event\u data\u display[$index]);session::set('event\u data\u display',$event\u data\u display');这很有效,谢谢。+1的回答session::forget('event\u data\u display.$i)
这……如何远程适用?
public function removeAddTocart(Request $request)
{
$remove = ''.$request->id.'';
if (Session::has('productCart'))
{
foreach (Session::get('productCart') as $key => $value)
{
if ($value === $remove)
{
Session::pull('productCart.'.$key); // retrieving the value and remove it from the array
break;
}
}
}
}
session()->save();