使用POST传递序列化数组会产生意外结果

时间:2016-07-06 13:13:50

标签: php arrays post multidimensional-array deserialization

我很困惑。

我的目标是将数组通过POST传递到另一个页面并访问它的数据。我找到了一种使用serialize()的方法。

它工作正常,因为我能够在另一页上看到数组,但是当我尝试访问主数组中的嵌套数组时,我没有得到预期的结果。换句话说,我可以访问主数组中的数据,但内部数组得到“null”。

让我告诉你我做了什么:

//The array:
    $cart = &JModelLegacy::getInstance('cart', 'jshop'); 
    $cart->load(); 

//I can access data of the inner array (the products) within the $cart array, for example:
    $productos = $cart->products;
    echo "<pre>".json_encode($cart, JSON_PRETTY_PRINT)."</pre>";        
    echo "<pre>".json_encode($productos[1], JSON_PRETTY_PRINT)."</pre>";        

//Then serializing it:
    $serializedcart = serialize($cart);

//Then sending it in a form using POST
    <input type='hidden' name='cartserialized' value='<?php print $serializedcart?>'>           

....然后在另一页上:

// I unserialize the transferred array:
$carretilla = unserialize($_POST[cartserialized]);

// And this doesn't work anymore, I get "null" for $productos:
$productos = $carretilla->products;
echo "<pre>".json_encode($carretilla, JSON_PRETTY_PRINT)."</pre>";      
echo "<pre>".json_encode($productos[1], JSON_PRETTY_PRINT)."</pre>";        

为什么呢?任何帮助将不胜感激。

这是序列化之前的输出:(购物车中的$ cart和产品的输出。)

{ "type_cart": "cart", "products": [ { "quantity": 1, "product_id": 329, "category_id": "17", "tax": null, "tax_id": "0", "product_name": "ATX Cromo Puro", "thumb_image": "thumb_882-2.jpg", "delivery_times_id": "0", "ean": "882-2", "attributes": "a:1:{i:1;i:28;}", "attributes_value": [ { "attr_id": 1, "value_id": 28, "attr": "Color", "value": "Cromo" } ], "extra_fields": [], "weight": "0.0000", "vendor_id": "1", "files": "a:0:{}", "freeattributes": "a:0:{}", "manufacturer": "Cross", "pid_check_qty_value": "A:218", "price": 570, "href": "\/index.php\/tienda\/product\/view\/17\/329", "free_attributes_value": [] }, { "quantity": 3, "product_id": 469, "category_id": "21", "tax": null, "tax_id": "0", "product_name": "Bater\u00eda Auxiliar", "thumb_image": "thumb_JK-PB035.jpg", "delivery_times_id": "0", "ean": "JK-PB035", "attributes": "a:0:{}", "attributes_value": [], "extra_fields": [], "weight": "35.0000", "vendor_id": "1", "files": "a:0:{}", "freeattributes": "a:0:{}", "manufacturer": null, "pid_check_qty_value": "P:469", "price": 265, "href": "\/index.php\/tienda\/product\/view\/21\/469", "free_attributes_value": [] } ], "count_product": 4, "price_product": 1365, "summ": 0, "rabatt_id": 0, "rabatt_value": 0, "rabatt_type": 0, "rabatt_summ": 0, "model_temp_cart": "tempcart", "price_product_brutto": 1365 } { "quantity": 3, "product_id": 469, "category_id": "21", "tax": null, "tax_id": "0", "product_name": "Bater\u00eda Auxiliar", "thumb_image": "thumb_JK-PB035.jpg", "delivery_times_id": "0", "ean": "JK-PB035", "attributes": "a:0:{}", "attributes_value": [], "extra_fields": [], "weight": "35.0000", "vendor_id": "1", "files": "a:0:{}", "freeattributes": "a:0:{}", "manufacturer": null, "pid_check_qty_value": "P:469", "price": 265, "href": "\/index.php\/tienda\/product\/view\/21\/469", "free_attributes_value": [] }

序列化和发送后:

{ "__PHP_Incomplete_Class_Name": "jshopCart", "type_cart": "cart", "products": [ { "quantity": 1, "product_id": 329, "category_id": "17", "tax": null, "tax_id": "0", "product_name": "ATX Cromo Puro", "thumb_image": "thumb_882-2.jpg", "delivery_times_id": "0", "ean": "882-2", "attributes": "a:1:{i:1;i:28;}", "attributes_value": [ { "attr_id": 1, "value_id": 28, "attr": "Color", "value": "Cromo" } ], "extra_fields": [], "weight": "0.0000", "vendor_id": "1", "files": "a:0:{}", "freeattributes": "a:0:{}", "manufacturer": "Cross", "pid_check_qty_value": "A:218", "price": 570, "href": "\/index.php\/tienda\/product\/view\/17\/329", "free_attributes_value": [] }, { "quantity": 3, "product_id": 469, "category_id": "21", "tax": null, "tax_id": "0", "product_name": "Bater\u00eda Auxiliar", "thumb_image": "thumb_JK-PB035.jpg", "delivery_times_id": "0", "ean": "JK-PB035", "attributes": "a:0:{}", "attributes_value": [], "extra_fields": [], "weight": "35.0000", "vendor_id": "1", "files": "a:0:{}", "freeattributes": "a:0:{}", "manufacturer": null, "pid_check_qty_value": "P:469", "price": 265, "href": "\/index.php\/tienda\/product\/view\/21\/469", "free_attributes_value": [] } ], "count_product": 4, "price_product": 1365, "summ": 0, "rabatt_id": 0, "rabatt_value": 0, "rabatt_type": 0, "rabatt_summ": 0, "model_temp_cart": "tempcart", "price_product_brutto": 1365 } null

2 个答案:

答案 0 :(得分:0)

为了在PHP中反序列化对象,您需要先加载该类。因此,请包含定义此对象类型的脚本,然后反序列化。

答案 1 :(得分:0)

谢谢你们。

我尝试使用其他论坛中建议的json_encode和json_decode,它似乎工作得更好,我在另一端发送和检索数据没有问题。

感谢您宝贵的时间。