douvcpx6526 2013-05-17 12:07
浏览 64
已采纳

如何取消或删除$ _SESSION中的一些嵌套数组

I have some items in shop basket in SESSION.

Each item maybe be for one shop and another item maybe for another shop.

I want when customer click in create factor button for each shop item list in basket. How can I delete or unset some items in my basket that have same shopid and customer click create factor for this shop with this shopid.

My session array for example is:

Array('customer' => Array('basket' => Array(
                    '9_2' => Array
                        (
                            "row" => "0",
                            'item' => 'cd',
                            'count' => '1',
                            'sale_start_date' => '1391-12-25 19:27:56',
                            'sale_end_date' => '1392-04-20 19:27:49',
                            'sale_price' => '40500',
                            'price' => '54564',
                            'id' => '999035',
                            'shopid' => '4'
                        ),
                        '999_17' => Array
                        (
                            'row' => '1',
                            'item' => 'car',
                            'count' => '1',
                            'sale_start_date' => '0000-00-00 00:00:00',
                            'sale_end_date' => '0000-00-00 00:00:00',
                            'sale_price' => '0',
                            'price' => '520000',
                            'id' => '999039',
                            'code' => 'b125nh',
                            'shopid' => '6'
                        ),
                        '9_3' => Array
                        (
                            'row' => '2',
                            'item' => 'book',
                            'count' => '1',
                            'sale_start_date' => '0000-00-00 00:00:00',
                            'sale_end_date' => '0000-00-00 00:00:00',
                            'sale_price' => '0',
                            'price' => '520000',
                            'id' => '999039',
                            'code' => 'b125nh',
                            'shopid' => '4'
                        ),
                        '10_5' => Array
                        (
                            'row' => '2',
                            'item' => 'dvd',
                            'count' => '1',
                            'sale_start_date' => '0000-00-00 00:00:00',
                            'sale_end_date' => '0000-00-00 00:00:00',
                            'sale_price' => '0',
                            'price' => '520000',
                            'id' => '999039',
                            'code' => 'b125nh',
                            'shopid' => '5'
                        )
                    )
                )
            );

You can see some items are with different shopid and it's not sort.

How can delete, for example, those items with shopid=4 from my basket?

  • 写回答

4条回答 默认 最新

  • dqv84329 2013-05-17 12:36
    关注

    Using a simple loop and built in PHP array functions:

    $deleteShopId = 4;
    $test = Array('customer' => Array('basket' => Array(
                        '9_2' => Array
                            (
                                "row" => "0",
                                'item' => 'cd',
                                'count' => '1',
                                'sale_start_date' => '1391-12-25 19:27:56',
                                'sale_end_date' => '1392-04-20 19:27:49',
                                'sale_price' => '40500',
                                'price' => '54564',
                                'id' => '999035',
                                'shopid' => '4'
                            ),
                            '999_17' => Array
                            (
                                'row' => '1',
                                'item' => 'car',
                                'count' => '1',
                                'sale_start_date' => '0000-00-00 00:00:00',
                                'sale_end_date' => '0000-00-00 00:00:00',
                                'sale_price' => '0',
                                'price' => '520000',
                                'id' => '999039',
                                'code' => 'b125nh',
                                'shopid' => '6'
                            ),
                            '9_3' => Array
                            (
                                'row' => '2',
                                'item' => 'book',
                                'count' => '1',
                                'sale_start_date' => '0000-00-00 00:00:00',
                                'sale_end_date' => '0000-00-00 00:00:00',
                                'sale_price' => '0',
                                'price' => '520000',
                                'id' => '999039',
                                'code' => 'b125nh',
                                'shopid' => '4'
                            ),
                            '10_5' => Array
                            (
                                'row' => '2',
                                'item' => 'dvd',
                                'count' => '1',
                                'sale_start_date' => '0000-00-00 00:00:00',
                                'sale_end_date' => '0000-00-00 00:00:00',
                                'sale_price' => '0',
                                'price' => '520000',
                                'id' => '999039',
                                'code' => 'b125nh',
                                'shopid' => '5'
                            )
                        )
                    )
                );
    
    foreach($test['customer']['basket'] as $something => $somethingElse){
    $temp = array_search($deleteShopId,$somethingElse);
       if($temp !== FALSE && $temp =='shopid'){
          unset($test['customer']['basket'][$something]);
       }
    }
    
    var_dump($test['customer']['basket']);
    

    output:

    array(2) {
      ["999_17"]=>
      array(10) {
        ["row"]=>
        string(1) "1"
        ["item"]=>
        string(3) "car"
        ["count"]=>
        string(1) "1"
        ["sale_start_date"]=>
        string(19) "0000-00-00 00:00:00"
        ["sale_end_date"]=>
        string(19) "0000-00-00 00:00:00"
        ["sale_price"]=>
        string(1) "0"
        ["price"]=>
        string(6) "520000"
        ["id"]=>
        string(6) "999039"
        ["code"]=>
        string(6) "b125nh"
        ["shopid"]=>
        string(1) "6"
      }
      ["10_5"]=>
      array(10) {
        ["row"]=>
        string(1) "2"
        ["item"]=>
        string(3) "dvd"
        ["count"]=>
        string(1) "1"
        ["sale_start_date"]=>
        string(19) "0000-00-00 00:00:00"
        ["sale_end_date"]=>
        string(19) "0000-00-00 00:00:00"
        ["sale_price"]=>
        string(1) "0"
        ["price"]=>
        string(6) "520000"
        ["id"]=>
        string(6) "999039"
        ["code"]=>
        string(6) "b125nh"
        ["shopid"]=>
        string(1) "5"
      }
    }
    

    Of course rename $something and $somethingElse to something more meaningful. I have no idea what they represent.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化