更新previous届阵列Laravel [英] Updating previous Session Array Laravel

查看:132
本文介绍了更新previous届阵列Laravel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我如何更新我的previous阵列的问题吗?
目前有什么发生在我的code是它只是增加新的session数组,而不是在这里更新申报关键是我的code:

 的foreach($ items_updated为$关键=> $添加)
{
    如果($ ID == $添加['ITEM_ID'])
    {
        $ newquantity = $添加['item_quantity'] - 1;
        $更新=阵列(
            ITEM_ID'=> $项目['ITEM_ID'],
            item_quantity'=> $ newquantity,
        );
    }
}会议::推('项目',$更新);


解决方案

  $项目=会话::得到('项目',[]);的foreach($项目,如:放; $项){
    如果($项目['ITEM_ID'] = =的$ id){
        $项目['item_quantity'] - ;
    }
}会议::设置('项目',$项目);

I have an issue on how can I update my Previous array ? What currently happening to my code is its just adding new session array instead of updating the declared key here's my code:

foreach ($items_updated as $key => $added)
{
    if ($id == $added['item_id'])
    {
        $newquantity = $added['item_quantity'] - 1;
        $update = array(
            'item_id' => $items['item_id'],
            'item_quantity' =>  $newquantity,
        );
    }
}

Session::push('items', $updated);

解决方案

$items = Session::get('items', []);

foreach ($items as &$item) {
    if ($item['item_id'] == $id) {
        $item['item_quantity']--;
    }
}

Session::set('items', $items);

这篇关于更新previous届阵列Laravel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆