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条)

报告相同问题?

悬赏问题

  • ¥20 Python安装cvxpy库出问题
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题