dongwei1855 2014-05-28 03:52
浏览 53
已采纳

在会话中添加相同数组键的数组值

I have an array structure that is stored in Session

edit : the number of item_id's is not definite. It maybe 1-3 items on one transaction and 2-20 items on the next.

"items": [{
    "item_id": "1",
    "item_quantity": "151"
}, {
    "item_id": "2",
    "item_quantity": "54"
}, {
    "item_id": "2",
    "item_quantity": "23"
}, {
    "item_id": "3",
    "item_quantity": "3"
}, {
    "item_id": "3",
    "item_quantity": "3"
}]

What I'm trying to achieve is to add up all item_quantity which has the same item_id

"items": [{
    "item_id": "1",
    "item_quantity": "151"
}, {
    "item_id": "2",
    "item_quantity": "77"
}, {
    "item_id": "3",
    "item_quantity": "6"
}]

This is what I have tried so far, but I'm not getting the desired result

Controller

public function Save() {
    if (Input::has('addItem')) {
        if (Session::has('items')) {
            Session::push('items', [
                'item_id' => Input::get('item_id'),
                'item_quantity' => Input::get('item_quantity')
            ]);
            $array = Session::get('items');

            foreach($array as $key => $value) {
                foreach($value as $item_id => $item_quantity) {
                    $total = array();

                    $id = $value['item_id'];

                    $quantity = $value['item_quantity'];

                    if (!isset($total[$id])) {
                        $total[$id] = 0;
                    }

                    $total[$id] += $quantity;
                    echo $total[$id];

                }
            } else {
                Session::put('items', [
                    0 => [
                        'item_id' => Input::get('item_id'),
                        'item_quantity' => Input::get('item_quantity')
                    ]
                ]);
            }

            $data = Session::all();

            //return $data;

            $item = Item::lists('item_name', 'id');
            return View::make('test')->with('data', $data)->with('items', $item);
     }
  • 写回答

2条回答 默认 最新

  • dongzhan1878 2014-05-28 05:17
    关注

    You're on the right track. See comments in the code.

    public function Save() {
      if (Input::has('addItem')) {
        if (Session::has('items')) {
          Session::push('items', [
              'item_id' => Input::get('item_id'),
              'item_quantity' => Input::get('item_quantity')
          ]);
          $array = Session::get('items');
          $total = array(); //move outside foreach loop because we don't want to reset it
    
          foreach ($array as $key => $value) {
    
              $id = $value['item_id'];
              $quantity = $value['item_quantity'];
    
              if (!isset($total[$id])) {
                $total[$id] = 0;
              }
    
              $total[$id] += $quantity;
              echo $total[$id];
          }
    
          //now convert our associative array from  array(actual_item_id => actual_item_quantity,....)
          //into array(array('item_id' => actual_item_id, 'item_quantity' => actual_item_quantity), ....)
          $items = array();
    
          foreach($total as $item_id => $item_quantity) {
            $items[] = array(
                'item_id' => $item_id,
                'item_quantity' => $item_quantity
            );
          }
    
          Session::put('items', $items);
        } else {
          Session::put('items', [
              0 => [
                  'item_id' => Input::get('item_id'),
                  'item_quantity' => Input::get('item_quantity')
              ]
          ]);
        }
    
        $data = Session::all();
    
        //return $data;
    
        $item = Item::lists('item_name', 'id');
        return View::make('test')->with('data', $data)->with('items', $item);
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?