具有POST值的PHP会话保存数组

时间:2015-06-15 08:24:08

标签: php arrays session multidimensional-array

首先抱歉我的英语。

我使用php $ _SESSION对象有一个奇怪的问题。 我花了2天没有找到解决方案。 我正在试图保存一个存储一些帖子值的多维数组。 我用这个值创建一个数组,然后我创建一个包含这个数组的多维数组。

第1页(多数组中的setvalues和会话中的保存) - > GET - >第2页阅读会议。

我的代码第1页:

首先,将post值保存到object属性中并验证它们。

public function validateData(){

        $this->nombres=$_POST["nombre"];
        $this->imp_nombres=$_POST["imp_nombre"];
        $this->numeros=$_POST["numero"];
        $this->imp_numeros=$_POST["imp_numero"];
        $this->tallas=$_POST["talla"];
        $this->cantidades=$_POST["cantidad"];
        $this->productos=$_POST["products"];
        $this->equipos=$_POST["equipo"];


        if(WSI_Funtions::compareSizes($this->nombres,$this->imp_nombres,$this->numeros,$this->imp_numeros,$this->tallas,$this->cantidades,$this->productos,$this->equipos))
        {
            $this->isValidModel=true;
            $this->saveProductsValues();

        }
        else{
          $this->isValidModel=false;
          $this->errorMessage="Los datos no son correctos. Los parametros no coinciden";
        }

}

如果数据正常,我保存这些值:

public function saveProductsValues()
{


    $this->productsValues=array();
    $this->productsValues["names"]=$this->nombres;
    $this->productsValues["imp_nombres"]=$this->imp_nombres;
    $this->productsValues["numeros"]=$this->numeros;
    $this->productsValues["imp_numeros"]=$this->imp_numeros;
    $this->productsValues["tallas"]=$this->tallas;
    $this->productsValues["cantidades"]=$this->cantidades;
    $this->productsValues["productos"]=$this->productos;
    $this->productsValues["equipos"]=$this->equipos;


}

然后我将其保存在会话中:

public function saveSessionValues()
{
    $_SESSION['customer'] = $this->customerObject;
    $_SESSION['productsValues'] =$this->productsValues;
    echo var_dump($_SESSION['productsValues']); 
}

saveSessionValues echo打印出来:

array(8) { ["names"]=> array(12) { [0]=> string(0) "" [1]=> string(12) "ÁNGEL HDEZ." [2]=> string(11) "VUJASINOVIC" [3]=> string(4) "ABIA" [4]=> string(10) "MUTAKABBIR" [5]=> string(8) "PETROVIC" [6]=> string(5) "DOBOS" [7]=> string(4) "HOMS" [8]=> string(6) "CASTRO" [9]=> string(0) "" [10]=> string(0) "" [11]=> string(0) "" } ["imp_nombres"]=> array(12) { [0]=> string(0) "" [1]=> string(1) "1" [2]=> string(1) "1" [3]=> string(1) "1" [4]=> string(1) "1" [5]=> string(1) "1" [6]=> string(1) "1" [7]=> string(1) "1" [8]=> string(1) "1" [9]=> string(0) "" [10]=> string(0) "" [11]=> string(0) "" } ["numeros"]=> array(12) { [0]=> string(1) "7" [1]=> string(1) "8" [2]=> string(1) "9" [3]=> string(0) "" [4]=> string(2) "11" [5]=> string(2) "12" [6]=> string(2) "18" [7]=> string(2) "19" [8]=> string(2) "22" [9]=> string(1) "5" [10]=> string(2) "33" [11]=> string(0) "" } ["imp_numeros"]=> array(12) { [0]=> string(1) "1" [1]=> string(1) "1" [2]=> string(1) "1" [3]=> string(0) "" [4]=> string(1) "1" [5]=> string(1) "1" [6]=> string(1) "1" [7]=> string(1) "1" [8]=> string(1) "1" [9]=> string(1) "1" [10]=> string(1) "1" [11]=> string(0) "" } ["tallas"]=> array(12) { [0]=> string(4) "XXXL" [1]=> string(3) "XXL" [2]=> string(2) "XL" [3]=> string(3) "XXL" [4]=> string(3) "XXL" [5]=> string(3) "XXL" [6]=> string(4) "XXXL" [7]=> string(3) "XXL" [8]=> string(2) "XL" [9]=> string(4) "XXXL" [10]=> string(2) "XL" [11]=> string(3) "XXL" } ["cantidades"]=> array(12) { [0]=> string(1) "1" [1]=> string(1) "1" [2]=> string(1) "1" [3]=> string(1) "1" [4]=> string(3) "145" [5]=> string(1) "1" [6]=> string(1) "1" [7]=> string(1) "1" [8]=> string(1) "1" [9]=> string(1) "1" [10]=> string(1) "1" [11]=> string(1) "1" } ["productos"]=> array(12) { [0]=> string(3) "109" [1]=> string(3) "109" [2]=> string(3) "109" [3]=> string(3) "109" [4]=> string(3) "109" [5]=> string(3) "109" [6]=> string(3) "109" [7]=> string(3) "109" [8]=> string(3) "109" [9]=> string(3) "109" [10]=> string(3) "109" [11]=> string(3) "109" } ["equipos"]=> array(12) { [0]=> string(7) "LEB ORO" [1]=> string(7) "LEB ORO" [2]=> string(7) "LEB ORO" [3]=> string(7) "LEB ORO" [4]=> string(7) "LEB ORO" [5]=> string(7) "LEB ORO" [6]=> string(12) "ES TALLA 4XL" [7]=> string(7) "LEB ORO" [8]=> string(7) "LEB ORO" [9]=> string(12) "ES TALLA 4XL" [10]=> string(7) "LEB ORO" [11]=> string(7) "LEB ORO" } }

我的代码页面2(省略了php标签):

if (!isset($_SESSION)) { session_start(); }
echo var_dump($_SESSION['productsValues']);

这个echo打印下一个值:

array(8) { ["names"]=> NULL ["imp_nombres"]=> NULL ["numeros"]=> NULL ["imp_numeros"]=> NULL ["tallas"]=> NULL ["cantidades"]=> NULL ["productos"]=> NULL ["equipos"]=> NULL }

存在第一级数组,因为打印了子数组键,但所有第二级数组都是NULL。

也许是为了使用$ _POST值? 我曾尝试编码$ _POST对象的值,只保存一个json字符串而不是保存一个具有相同结果的对象,JSON中的第一个节点是数组的键,但值为“NULL”

有任何帮助吗? 谢谢!

1 个答案:

答案 0 :(得分:2)

你正在写这些价值观。试试 -

$_SESSION['productsValues'][] = $this->productsValues;