在codeigniter会话中向数组添加值

时间:2018-02-05 15:36:08

标签: php arrays codeigniter session

我正在尝试通过设置数组在codeigniter中创建一个会话我可以设置数组但是当我尝试输入另一个数组时它会覆盖第一个数组

   <?php
    $array = array(
    'delivery_address' => array(
    'house_name' => $house_name,
    'address1' => $address1,
    'address2' => $address2,
    'city' => $city,
    'county' => $county,
    'country'=> $country,
    'postcode' => $postcode

     )

     );
     $this->session->userdata($array);
     $result = $this->session->userdata('delivery_address');
     print_r($result);
     ?>

我想我正在寻找的是当我回去用

设置另一个数组时
      $array = array('delivery_address'=>array(
        )

     );

它有多个值我可以循环 感谢

4 个答案:

答案 0 :(得分:0)

$array = array('delivery_address'=>array( ));

总是会被覆盖

$array[] = array('delivery_address'=>array( ));  

这将添加

  

array('delivery_address'=&gt; array()

作为第一个数组中的新元素。

答案 1 :(得分:0)

假设这是您的数组

$array = array(
    'delivery_address' => array(
        'house_name' => 'aa',
        'address1' => 'bbb',
        'address2' => 'ccc'
    )
);
$array['delivery_address']['house_name'] = 'new Value';
print_r($array);

<强>输出

Array ( 
    [delivery_address] => Array ( 
        [house_name] => new Value 
        [address1] => bbb 
        [address2] => ccc ) 
    )

答案 2 :(得分:0)

您可以改为创建如下会话:

$this->session->set_userdata('session1', array1());

这将保留您创建的第一个会话。

$this->session->set_userdata('session2', array2());
然后打电话给你的会话,如:

$session1 = $this->session->userdata('session1');
$session2 = $this->session->userdata('session2');

答案 3 :(得分:0)

    **I had a similar problem way back but I just had to enter 5 arrays in session
    Codeigniter overrides the array if there is an array already in session so I came up
 with this little Nasty Code but it works 100% but it has limitations **


      if($this->session->userdata('delivery_address1')==false) {
                $this->session->set_userdata('delivery_address1', $array);
            }
            elseif ($this->session->userdata('delivery_address1')==true && $this->session->userdata('delivery_address2')==false){
                $this->session->set_userdata('delivery_address2', $array);
            }
            elseif ($this->session->userdata('delivery_address1')==true && $this->session->userdata('delivery_address2')==true && $this->session->userdata('cart_delivery_address')==false){
                $this->session->set_userdata('delivery_address3', $array);
            }
            elseif ($this->session->userdata('delivery_address1')==true && $this->session->userdata('delivery_address2')==true && $this->session->userdata('delivery_address3')==true && $this->session->userdata('delivery_address4')==false){
                $this->session->set_userdata('delivery_address4', $array);
            }
            elseif ($this->session->userdata('delivery_address1')==true && $this->session->userdata('delivery_address2')==true && $this->session->userdata('delivery_address3')==true && $this->session->userdata('delivery_address4')==true && $this->session->userdata('delivery_address5')==false){
                $this->session->set_userdata('delivery_address5', $array);
            }
            elseif($this->session->userdata('delivery_address1')==true && $this->session->userdata('delivery_address2')==true && $this->session->userdata('delivery_address3')==true && $this->session->userdata('delivery_address4')==true && $this->session->userdata('delivery_address5')==true) {
                //do something here

            }